Nidheesh
Nidheesh

Reputation: 4562

TypeError: Cannot read property 'search' of undefined for $location.search()

How can I get the below param in the params: list below. I am getting the below error,

TypeError: Cannot read property 'search' of undefined
    at Object.async (abcd.html?kId=pqr)

url:

http://localhost:8080/.........../abcd.html?kId=pqr

Code:

var app = angular.module('angModule', [], function($locationProvider) {
    $locationProvider.html5Mode(true);
});
app.factory(
    'myService',
    function($http,$location) {
        return {
            async : function() {
                return $http
                    .get('http://localhost:8080/...........,,,,./...',{params:{"kId": $location.search()['kId'] }});
                }
            };
        });

Upvotes: 0

Views: 1238

Answers (1)

martin
martin

Reputation: 96891

I think you're not defining the service correctly, that's why $location is undefined. You have to use notation:

batchModule.factory('myService', ['$interval', '$log', function($interval, $log) {

See "Dependencies" section in https://docs.angularjs.org/guide/services. In you case that would be:

app.factory('myService', ['$http', '$location', function($http, $location) {
    // ...
}]);

Upvotes: 1

Related Questions