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的更多相关文章

  1. Directive Definition Object

    不知道为什么这个我并没有想翻译过来的欲望,或许我并没有都看熟透,不好误人子弟,原版奉上. Here's an example directive declared with a Directive D ...

  2. angular directive 深入理解

    由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...

  3. AngularJS的directive(指令)配置选项说明

    js代码如下: var appModule = angular.module("appModule", []); appModule.controller("Ctrl&q ...

  4. directive(指令里的)的compile,pre-link,post-link,link,transclude

    The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of comp ...

  5. AngularJS.directive系列:嵌套directive的通讯及scope研究

    一.directive中的scope directive无疑是AngularJS中比较复杂难懂的部分,而directive中个scope更是其中最复杂的部分了,尤其是在嵌套directive中互相通讯 ...

  6. Angular1.x directive(指令里的)的compile,pre-link,post-link,link,transclude

    The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of comp ...

  7. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

  8. Unity3D 系统宏

    Platform Defines The platform defines that Unity supports for your scripts are:     Property: Functi ...

  9. 理解angularjs的作用域

    <!doctype html> <html ng-app="myApp"> <head> <script src="http:/ ...

随机推荐

  1. PHP使用header+Location实现网站301重定向

    对于我们SEO人员来说,有的时候需要对网站进行重定向.一般来说,对网站重定向的http返回状态码是301和302两种.下面兰州SEO就通过实例来为你介绍一下怎样通过PHP使用header+Locati ...

  2. php攻击漏洞总结

    1.两字节编码(gbk)都存在宽字节攻击问题[character_set_client=gbk] 案例:http://www.cnblogs.com/lcamry/articles/5625276.h ...

  3. 178. Rank Scores

    问题描述 解决方案 select sc.Score, (select count(*) from (select distinct Score from Scores ) ds where ds.Sc ...

  4. Python — 多线程与多进程

    1.多线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位,一个进程可以包含多个线程.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线 ...

  5. 最实用的 Linux 命令行使用技巧

    我们可能每天都会要使用到很多的 Linux 命令行. 我们也会网络上知晓一些使用它们的小技巧,但是如果我们没有时常来进行练习,就有可能会忘掉怎么去使用它们. 所以我就决定把那些你可能会忘记的小提示和小 ...

  6. 《Advanced Bash-scripting Guide》学习(九):备份最后一天所有修改的文件

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 Example 3-4. 备份最后一天所有修改的文件 #!/bin/bash # ...

  7. excel中日期设置星期

    在设置日期格式中-自定义中-设置填入yyyy-mm-dd [$-804]aaa;@ 即可.

  8. kylin_学习_02_kylin使用教程

    一. 二.参考资料 1.kylin从入门到实战:实际案例

  9. 获得客户端ip

    获得客户端ip private function GetIP(){        if(!empty($_SERVER["HTTP_CLIENT_IP"])){          ...

  10. 2、Monkey简单使用

    1.使用Monkey测试,前提是有虚拟机或者真机设备,查看是否有设备存在:adb devices (需要先进入SDK的tool目录下才执行该操作) 2.查看设备上各个包名 adb shell pm l ...