我需要一个像$routeChangeSuccess这样的事件,但需要$location.search()变量.我在调用$location.search(‘newview’),需要一种方法来知道它何时发生变化.
谢谢!
解决方法
你应该使用$scope.$watch:
$scope.$watch(function(){ return $location.search() },function(){ // reaction });
在Angular docs中查看更多关于$watch的信息.
如果您只是想要添加’newview’,那么对于查询字符串,上述代码将起作用.
i.e. from
http://server/page
tohttp://server/page?newvalue
但是,如果您要查找“newview”中的更改,则上述代码将无效.
i.e from
http://server/page?newvalue=a
tohttp://server/page?newvalue=b
您需要使用’objectEquality’参数来调用$watch.这导致$watch使用值相等,而不是对象引用相等.
$scope.$watch(function(){ return $location.search() },function(){ // reaction },true);
注意添加第3个参数(true).