AngularJS搜索更改事件 [英] AngularJS Search Change Event
问题描述
我需要像 $ routeChangeSuccess
的事件,但对于$ location.search()变量。我打电话$ location.search('NewView的'),并需要一种方法来知道什么时候改变了。
I need an event like $routeChangeSuccess
but for the $location.search() variable. I am calling $location.search('newview'), and need a way to know when it changes.
谢谢!
推荐答案
您应该使用 $ $范围看
:
$scope.$watch(function(){ return $location.search() }, function(){
// reaction
});
查看更多有关 $观看
在角文档
如果你只是在寻找除NewView的,要查询字符串上面code会工作。
If you are just looking for the addition of 'newview', to the query string the above code will work.
即。从的http://服务器/页
到的http://服务器/页NEWVALUE
不过,如果你正在寻找'NewView的'改变上述code将无法工作。
However, if you are looking for a change in 'newview' the above code will not work.
即由的http://服务器/页NEWVALUE = A
到的http://服务器/页NEWVALUE = B
您将需要使用'objectEquality参数来调用$手表。这将导致$观赏利用价值的平等,而不是对象引用相等。
You will need to use the 'objectEquality' param to the call to $watch. This causes $watch to use value equality, instead of object reference equality.
$scope.$watch(function(){ return $location.search() }, function(){
// reaction
}, true);
注意,除了第三个参数的(真)。
Notice the addition of 3rd param (true).
这篇关于AngularJS搜索更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!