zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    iOS_核心动画(二)
    iOS_核心动画CALayer(一)
    iOS_KVC与KVO
    iOS_Quartz 2D绘图
    iOS_触摸事件与手势识别
    iOS_多线程(二)
    iOS_多线程(一)
    iOS_UIAlertController
    CTF-Pwn-[BJDCTF 2nd]diff
    数据结构--队列(Java实现)
  • 原文地址:https://www.cnblogs.com/qook/p/6814114.html
Copyright © 2011-2022 走看看