Muhsin Mohamed Pc Muhsin Mohamed Pc - 1 year ago 86
AngularJS Question

$location service not passing URL Parameter to Controller

I am trying to pass an ID via URL parameter to a page named "more.html". So the URL Parameter will like the following.

http://example.com/more.html?id=200


I used $location service in my controller on the more.html page to get the Parameter ID from the URL. Here is the controller code that I used.

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($location, $scope, $http) {
var sno = $location.search().id;
alert(sno);
});


The result I got from the above code was "Undefined".

Answer Source

Change you url structure as following (adding #)

from:

http://example.com/more.html?id=200

to:

http://example.com/more.html#?id=200

Adding the # more.html and before your url parameters will fix the problem. Maybe for your next project you could make use of ui-router, which is a more structured way of using parameters.

https://github.com/angular-ui/ui-router

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download