最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jan 30, 2016 at 12:27 Smita Ahinave 1,8887 gold badges26 silver badges42 bronze badges asked Jan 30, 2016 at 12:22 sajalsurajsajalsuraj 99217 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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) {
发布评论

评论列表(0)

  1. 暂无评论