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.

时间: 2024-12-25 09:33:09

How to watch property in attrs of directive的相关文章

angular directive 深入理解

由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In an AngularJS directive the scope allows you to access the data in the attributes of the element to which the directive is applied 这句话,感觉道出了diretive的原

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

The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of compile and link functions inside AngularJS directives part 2: transclusion [译]ng指令中的compile与link函数解析 AngularJS directives are amazing. They allow you to

angular学习笔记(十九)

本篇主要介绍angular使用指令修改DOM: 使用angular指令可以自己扩展html语法,还可以做很多自定义的事情.在后面会专门讲解这一块的知识,这一篇只是起到了解入门的作用. 与控制器,过滤器,服务,一样,可以通过模块实例的directive的方法来创建指令: var someModule = angular.module('SomeModule',[]); someModule.directive('directiveName',function(){ return { link: f

Unity3D 系统宏

Platform Defines The platform defines that Unity supports for your scripts are:     Property: Function: UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code. UNITY_EDITOR_WIN #define directive for Editor code on Windows

Unity 平台依赖编译

位置:unity文档-Manual-Scripting-Platform dependent compilation Property: Function: UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code. UNITY_EDITOR_WIN #define directive for Editor code on Windows. UNITY_EDITOR_OSX #defin

Vue 使用中的小技巧

在vue的使用过程中会遇到各种场景,当普通使用时觉得没什么,但是或许优化一下可以更高效更优美的进行开发.下面有一些我在日常开发的时候用到的小技巧,在下将不定期更新~ 1.多图表resize事件去中心化 1.1 一般情况 有时候我们会遇到这样的场景,一个组件中有几个图表,在浏览器resize的时候我们希望图表也进行resize,因此我们会在父容器组件中写: mounted() { setTimeout(() => window.onresize = () => { this.$refs.char

vue使用小技巧

1.多图表resize事件去中心化 1.1 一般情况 有时候我们会遇到这样的场景,一个组件中有几个图表,在浏览器resize的时候我们希望图表也进行resize,因此我们会在父容器组件中写: mounted() { setTimeout(() => window.onresize = () => { this.$refs.chart1.chartWrapperDom.resize() this.$refs.chart2.chartWrapperDom.resize() // ... }, 20

Scope Directive

---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/scope What are Scopes? Scope is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in hiera

angularjs directive 实例 详解

前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的model,也可以理解成插件,只不过这种model是带html的,例如:php的分页函数. 一,angularjs directive的常用格式,以及参数说明 1,直接return phonecatDirectives = angular.module('phonecatDirectives', [])