AngularJS搜索更改事件 [英] AngularJS Search Change Event

查看:99
本文介绍了AngularJS搜索更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像 $ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆