My routeProvider for reloadOnSearch route is set to false :
$routeProvider .when( "/film/list", { templateUrl: '/film/list.html', controller: FilmListController, reloadOnSearch: false } .... )
I did this because I do not want the entire controller to restart after changing the query string, but I still use it and the URL looks like this: film/list?sort=isbn&order=asc&offset=0 . Now, when the query string changes, I want to call a function from my controller. So I tried setting up $ watch in the controller:
$scope.location = $location; $scope.$watch( 'location.search()', function( search) { $scope.list(); });
But that does not work. If I set $ watch to view changes to a single query string parameter, then it works. But I do not want to set $ watch for each parameter of my query string. Now I use the following:
$scope.location = $location; $scope.$watch( 'location.url()', function( url ) { if($location.path() == '/film/list') $scope.list(); });
Therefore, instead of looking for a search, I look at the entire URL and then check if the path is the one I set in routeProvider to conditionally call the function. What I don't like about this solution is that I have to explicilty enter the value for $ location.path to match.
Could someone suggest a better solution and maybe explain to me why $ watch does not work for $ location.search ()?
angularjs listener watch
MBozic Feb 26 '13 at 16:13 2013-02-26 16:13
source share