rakete rakete - 5 months ago 35
AngularJS Question

$routeParams.param returns undefined

I've configured a route and try to pass parameters to a controller:

app.js

.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('spot', {
url: "/spot/:param",
templateUrl: "templates/spot.html",
controller: "SpotCtrl"
});
$urlRouterProvider.otherwise('/');
});


controller:

angular.module('spotx.controllers')
.controller('SpotCtrl', ['$scope', '$routeParams',
function($scope, $routeParams) {

$scope.do = function() {
console.log($routeParams.param);
};

}])


The url is /#/spot/1111, but if I fire the do-function I get undefined in the console.

Answer

$routeParams is for ngRoute router not angular-ui-router.

You need $stateParams ... see ui router docs

I'm suprised you aren't getting an unknown provider error unless you have both router scripts included in page and have ngRoute also injected in a module

Comments