How to watch property in attrs of directive

Posted the important thing is not to

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How to watch property in attrs of directive相关的知识,希望对你有一定的参考价值。

Q:

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.

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

 

A:

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:

Interpolating the attribute

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.

以上是关于How to watch property in attrs of directive的主要内容,如果未能解决你的问题,请参考以下文章

How to Access Data in a Property Tree

jackson 序列化忽略未知字段: How to Ignore Unknown Properties While Parsing JSON in Java

How to update BOL entity property value via ABAP code

How To Use the AWK language to Manipulate Text in Linux

How to Create Triggers in MySQL

[转]How to add new table in NopCommerce