Passing a parameter inside $ location.path in Angular

I'm just trying to use $location.path() in my controller, but also passing a custom variable as a parameter. So it would look something like this, I think:

 $scope.parameter = 'Foo'; $location.path('/myURL/' + $scope.parameter); 

But that does not work. Does anyone know how this should be done in Angular?

+79
javascript angularjs
Mar 27 '14 at 13:13
source share
4 answers

to add parameters, you must use the $location.search() method to:

 $location.path('/myURL/').search({param: 'value'}); 

$location methods are chained.

this gives:

 /myURL/?param=value 
+155
Mar 27 '14 at 13:15
source share

Another way to add a parameter to the url:

  $location.path('/myURL/'+ param1); 

and you can define a route to myPage.html:

 config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/myURL/:param1', { templateUrl: 'path/myPage.html', controller: newController }); }]); 

Then in newController you can access the parameter:

 var param1= $routeParams.param1; 
+36
Jun 23 '14 at 14:38
source share

For example, if you need to specify one or more parameters in your URL:

 $location.path('/path').search({foo: 'valueFoo', baz:'valueBaz'}) 

in your url will represent

 /path?foo=valueFoo&baz=valueBaz 

To get parameters in another controller:

 var urlParams = $location.search(); urlParams.foo will return valueFoo urlParams.baz will return valueBaz 
+8
Jan 23 '17 at 18:10
source share
  function pathToSomewhere() { $stateParams.name= vm.name; //john $stateParams.phone= vm.phone; //1234 $stateParams.dateOfBirth= getDoB(); //10-10-1990 $location.path("/somewhere/").search($stateParams); }; 

This results in a url

 http://middle-of-nowhere.com/#/somewhere/?name=john&phone=1234&dateOfBirth=10-10-1990 

This way you do not need to manually enter the parameters inside the brackets

+2
May 25 '17 at 12:30 a.m.
source share



All Articles