I am trying to read multiple parameters from the url in angularjs? I am able to get the first parameter target as 34 but the second parameter is coming as undefined.
Asked
Active
Viewed 320 times
0
-
I am trying to read multiple parameters from the url in angularjs? I tried the following url http://localhost:48716/#?target=34#?testvalue=95 $rootScope.$watch('location.search()', function () { $rootScope.target = ($location.search()).target; $rootScope.testvalue= ($location.search()).testvalue; }, true); I am able to get the first parameter target as 34 but the second parameter is coming as undefined. – user1673394 Apr 04 '14 at 10:35
-
Add this into the question. – raina77ow Apr 04 '14 at 10:36
1 Answers
0
edit
You are not using #, ? etc the standard way and the angularJS will obviously not get what is meant by your URL params. There can only be one # and ? in the URL. Just learn about location hash, query strings etc and then the code might work correctly.
May be the URL should be localhost:48716/#?target=34&testvalue=95
old answer
You could use the $routeParams service to get the URL parameters. Then in your code you can simply look for $routeParams.target
and$routeParams.testvalue
to get the parameter values
Also if you want to monitor the route change, you should subscribe to routeChangeStart event instead of watching 'location.search()'.Look at How to watch for a route change in AngularJS?

Community
- 1
- 1

Ranjith Ramachandra
- 10,399
- 14
- 59
- 96
-
Hello, I tried the below code but I am getting both the values as undefined. var App = angular.module('myapp', []); App.run(function ($rootScope, $location, $routeParams) { alert($routeParams.target); alert($routeParams.testvalue); }); – user1673394 Apr 04 '14 at 11:03
-
May be it takes a while before the route can be known. use a timeout. angular.module('myapp', []); App.run(function ($rootScope, $location, $routeParams, $timeout) { $timeout(function(){alert($routeParams.target); alert($routeParams.testvalue); }, 1000})); – Ranjith Ramachandra Apr 04 '14 at 11:06
-
I tried as suggested by you but still facing the same issue both the values are coming as undefined. – user1673394 Apr 04 '14 at 11:19
-
Hey sorry for not noticing it earlier. But the syntax of query param in your URL is wrong. Fix that and your issues will be gone – Ranjith Ramachandra Apr 04 '14 at 12:03