admin管理员组文章数量:1312825
A very short question: In Angular, is there any way whatsoever to obtain the "root cause" of a $watch
to be triggered?
Let's say you have the following JavaScript code:
$scope.$watch("foo", function(value){
// here I'd like to know if the change is triggered
// due to change in ngModel or through ngClick
};
$scope.changeFoo = function(){
$scope.foo = "bar"
};
And this is the HTML:
<input ng-model="foo">
<button ng-click="changeFoo()">Change Foo > Bar</button>
In my $watch
I'd like to know what caused it to be fired. In this case, was it a change in ngModel
or was my value changed in the function of ngClick
?
A very short question: In Angular, is there any way whatsoever to obtain the "root cause" of a $watch
to be triggered?
Let's say you have the following JavaScript code:
$scope.$watch("foo", function(value){
// here I'd like to know if the change is triggered
// due to change in ngModel or through ngClick
};
$scope.changeFoo = function(){
$scope.foo = "bar"
};
And this is the HTML:
<input ng-model="foo">
<button ng-click="changeFoo()">Change Foo > Bar</button>
In my $watch
I'd like to know what caused it to be fired. In this case, was it a change in ngModel
or was my value changed in the function of ngClick
?
- 3 I'm not 100% sure but I don't think there's a way to know. Couldn't prove it though. Interesting question – edi9999 Commented May 19, 2014 at 10:04
2 Answers
Reset to default 6No, there is no way to do that with the current implementation.
$watch
are triggered running a function called $apply
. That function doesn't know who triggered it.
You are going to need another perspective to fix your problem.
No, we cant know which triggers it. Check this question how-to-get-the-dom-element-that-triggered-ng-change, maybe trying to write a custom directive to pass more info to your controller is a possible way to get trigger.
本文标签: javascriptAngular obtain cause of a watch to be triggeredStack Overflow
版权声明:本文标题:javascript - Angular: obtain cause of a $watch to be triggered - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741895216a2403536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论