gpt4 book ai didi

search - AngularJS 搜索更改事件

转载 作者:行者123 更新时间:2023-12-03 18:26:36 26 4
gpt4 key购买 nike

我需要一个类似 $routeChangeSuccess 的事件但是对于 $location.search() 变量。我正在调用 $location.search('newview'),并且需要一种方法来知道它何时发生变化。

谢谢!

最佳答案

您应该使用 $scope.$watch :

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

查看更多关于 $watch在 Angular docs .

如果您只是想在查询字符串中添加“newview”,则上述代码将起作用。

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



但是,如果您正在寻找“newview”中的更改,则上述代码将不起作用。

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



您将需要使用“objectEquality”参数来调用 $watch。这导致 $watch 使用值相等,而不是对象引用相等。

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

请注意添加了第三个参数 (true)。

关于search - AngularJS 搜索更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com