What is the correct Angular approach to retrieving the URL parameters?
Example: http://example.com/mypage.html?product=1234®ion=4&lang=en
Thanks
What is the correct Angular approach to retrieving the URL parameters?
Example: http://example.com/mypage.html?product=1234®ion=4&lang=en
Thanks
This will convert your query into an object
var queryData = url.split('?')[url.split('?').length - 1].split('&').reduce(function(prev, curr){
var fieldName = curr.split('=')[0];
var value = curr.split('=').length > 1 ? curr.split('=')[1] : '';
prev[fieldName] = value;
return prev
}, {});
And then you can access them by queryData.product, for example. Not angular, but it's a solution.
For the angular way, you can use $location.search() as described here http://www.angulartutorial.net/2015/04/get-url-parameter-using-angular-js.html
You can use the $location service. For example:
angular.module('parameters', []).run(['$location',
function($location) {
var params = $location.search();
}
]);
I posted the same question with more details, and found a perfect response. Please see DavidL's response in my question here:
Reading URL parameters in AngularJS - A simple way?
Thanks to everyone who assisted me in this thread; perhaps I didn't include enough details in the original question.