是否有可能'看'属性'的变化 [英] Is it possible to 'watch' attributes' changes

查看:94
本文介绍了是否有可能'看'属性'的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能手表上的指示用户界面的变化?
这样的事情:

  .directive('vValidation',函数(){
    返回功能(范围,元素,ATTRS){
        元素。$表(函数(){
            如果(this.hasClass('SomeClass的'))的console.log('SomeClass的加');
        });
    }
})


解决方案

是的。您可以使用 ATTR。$观察如果您在属性中使用插值。

但是,如果这不是一个插值属性,你希望它从别的地方在应用程序(极度不推荐什么改变的,读的常见缺陷),比你 $观看函数返回:

$范围手表(函数(){返回element.attr('类');},功能(newValue)以{});

总之,它可能是你最好的办法是改变code,改变的元素类。哪一刻它得到改变?

Is it possible to "watch" for ui changes on the directive? something like that:

.directive('vValidation', function() {
    return function(scope, element, attrs) {
        element.$watch(function() {
            if (this.hasClass('someClass')) console.log('someClass added');
        });
    }
})

解决方案

Yes. You can use attr.$observe if you use interpolation at the attribute.

But if this is not an interpolated attribute and you expect it to be changed from somewhere else in the application (what is extremely not recommended, read Common Pitfalls), than you can $watch a function return:

scope.$watch(function() {return element.attr('class'); }, function(newValue){});

Anyway, its probably that the best approach for you would be change the code that changes the element class. Which moment does it get changed?

这篇关于是否有可能'看'属性'的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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