Finding Changes in AngularJS

I need an event like $routeChangeSuccess , but for the variable $ location.search (). I call $ location.search ('newview') and need to know when it will change.

Thank!

+4
angularjs search
Feb 14 '13 at 4:24
source share
2 answers

You should use $scope.$watch :

 $scope.$watch(function(){ return $location.search() }, function(){ // reaction }); 

Learn more about $watch in Angular docs .

If you're just looking for the addition of "newview", the code above will be used to the query string.

i.e. from http://server/page to http://server/page?newvalue

However, if you are looking for a change in 'newview', the above code will not work.

ie from http://server/page?newvalue=a to http://server/page?newvalue=b

You will need to use the 'objectEquality' parameter to call $ watch. This leads to the fact that $ watch uses equality of value, instead of equality of references to objects.

 $scope.$watch(function(){ return $location.search() }, function(){ // reaction }, true); 

Note the addition of the third parameter (true).

+15
Feb 14 '13 at 5:51
source share

You can listen to the $ routeUpdate event in the controller:

 $scope.$on('$routeUpdate', function(){ $scope.sort = $location.search().sort; $scope.order = $location.search().order; $scope.offset = $location.search().offset; }); 
+1
Nov 22 '16 at 6:13
source share



All Articles