How to use current url params in angular controller

vzhen picture vzhen · Jul 3, 2013 · Viewed 51.5k times · Source

I have a dynamic value params in url and I want to get it and use in controller when onload But I have no idea how to deal with $route.current.params

Let say I have this route provider

$routeProvider.when('foo/:bar', {templateUrl: 'template.html'};

Then in my controller

app.controller('mapCtrl', function($scope, $route, $routeParams) {

  var param = $route.current.params.bar;
  console.log(param);

});

If user access foo/abcdefg suppose should show abcdefg in console but I got error. 'Cannot read property 'params' of undefined'

Answer

JAYBEkster picture JAYBEkster · Jul 3, 2013
$routeProvider.when('foo/:bar', {templateUrl: 'template.html', controller: 'mapCtrl'};
app.controller('mapCtrl', function($scope, $route, $routeParams) {

  var param = $routeParams.bar
  console.log(param);

});