Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing 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 guess:

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

But that doesn't work. Anyone know how this is supposed to be done in Angular?

like image 580
aadu Avatar asked Mar 27 '14 13:03

aadu


People also ask

What is location absUrl ()?

absUrl(); This method is getter only. Return full URL representation with all segments encoded according to rules specified in RFC 3986.

What is Location Search () in AngularJs?

HTML. Output: search() Method: It is a read and writes method of $location. It returns the current search parameters of the URL when passed without parameters and when passed with parameters it returns the $location object.

What is html5Mode?

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents.

What is the use of location service in angular?

Location is a service available in Angular 2+ apps that makes it easy to interact with the current URL path. For most navigation needs, the Angular router is the correct solution, but in a few cases the location service is needed to affect the url without involving the router.


2 Answers

to add Parameters you should use $location.search() method so:

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

$location methods are chainable.

this produce :

/myURL/?param=value 
like image 70
mohsen dorparasti Avatar answered Sep 20 '22 09:09

mohsen dorparasti


The another way to add parameter to url is:

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

and you can define route to myPage.html:

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

The parameter can then be accessed in newController as follows:

var param1= $routeParams.param1; 
like image 22
krzysiek.ste Avatar answered Sep 23 '22 09:09

krzysiek.ste