如何观看指令ATTRS财产 [英] How to watch property in attrs of directive

查看:139
本文介绍了如何观看指令ATTRS财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有从时间改变为时间的计数器的控制器。结果
这计数器绑在指令的属性和读取指令的链接函数内。

I have a controller that has a counter that changes from time to time.
That counter is tied to an attribute of a directive and read inside the link function of that directive.

我怎么能有一个指令运行函数中的每个attr属性值发生变化时?

How can I have a directive run a function each time that attr value changes?

感谢。

推荐答案

在您相应的链接功能:(假设你的属性称为计数和你的范围变量为:范围

Inside your corresponding link function: (assuming your attribute is called counter and your scope variable is: scope)

scope.$watch(attrs.counter, function (newTime) {
                    //do something with the new Time
});

其他的方式,肯定更有效的方式:

Other way, surely more efficient way:

里面的指令,将范围属性如下(将被隔离的话):

Inside your directive, set the scope property as following (it will be isolated so):

scope: { counter: '@'}

计数将自动观测,只要提供的电流值作为链接函数被调用。

The counter would automatically be observed providing the current value as long as the link function is called.

'@'好过=在这里,因为你,我想你不设置柜台在你的指令一个新值,这意味着你刚才读它。事实上, = 是双向数据绑定,但你可能并不需要它更有用。

'@' better than '=' here since you I suppose you don't set the counter to a new value in your directive, meaning you just read it. Indeed, = is more useful for two-way data binding but you might not need it.

这篇关于如何观看指令ATTRS财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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