admin 管理员组文章数量: 1086019
I'm writing a directive which needs to watch for elements that get updated with a particular class, say .ng-invalid
. As you know, .ng-invalid
is added to form elements which are invalid.
I need to watch these elements to determine if the class was added or removed.
How can I achieve this?
Thanks in advance
I'm writing a directive which needs to watch for elements that get updated with a particular class, say .ng-invalid
. As you know, .ng-invalid
is added to form elements which are invalid.
I need to watch these elements to determine if the class was added or removed.
How can I achieve this?
Thanks in advance
Share Improve this question edited Apr 30, 2013 at 23:01 lucuma 18.3k4 gold badges67 silver badges95 bronze badges asked Apr 30, 2013 at 9:10 AbilashAbilash 6,0896 gold badges27 silver badges30 bronze badges 2- could you share your code with fiddle or plunker demo – Ajay Singh Beniwal Commented Apr 30, 2013 at 9:32
- Here is a dummy fiddle for my requirement. jsfiddle/aW7FD – Abilash Commented Apr 30, 2013 at 11:19
2 Answers
Reset to default 6You could $watch a function that gets the length of $(".ng-invalid")
:
scope.$watch(function() {
return $(".ng-invalid").length;
}, function (newVal, oldVal) {
if(newVal !== oldVal) {
console.log('changed!', newVal, oldVal);
// do your changes here
}
})
Fiddle. In the fiddle, I added ng-minlength="2"
to the first input
. Type two characters into that field to see the $watch trigger.
Would it be sufficient for your purposes to watch the $invalid
attribute of FormController? This will notify you of changes to the form's holistic invalid status, for example:
// Somewhere in your directive; formCtrl is the FormController
scope.$watch(function() {
return formCtrl.$invalid;
}, function(isInvalid, wasInvalid) {
// ...
});
本文标签: javascriptHow To Watch a JQuery Selector Using AngularJS scopewatch() MethodStack Overflow
版权声明:本文标题:javascript - How To Watch a JQuery Selector Using AngularJS scope.$watch() Method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744082703a2530548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论