Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$location.search() is not working - AngularJs 1.5

I want to get the query string values. I am using $location.search() to get those values but it says that $location.search is not a function. I am using 1.5 version of AngularJs.

JS -

var app = angular.module('myApp', []);
app.config(['$locationProvider', function($locationProvider){
  $locationProvider.html5Mode(true);   
}]);


app.controller('myCtrl',[ '$location','$scope', function($scope, $location){
       var searchObject = $location.search();   
       console.log('searchObject');   
       console.log(searchObject); 
}]);

I don't understand what I am missing in the code.

like image 453
sajalsuraj Avatar asked Jan 30 '16 12:01

sajalsuraj


1 Answers

Of course it's not a function, because you are calling search method on the $scope object. The order of the services you inject into controller is $location then $scope. So what you called $location in controller is actually a $scope. Order is important.

Correct dependency injection should be:

[ '$location', '$scope', function($location, $scope) {
like image 84
dfsq Avatar answered Oct 19 '22 08:10

dfsq