Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch property in attrs of directive

Tags:

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?

Thanks.

like image 933
Francisc Avatar asked Dec 05 '13 14:12

Francisc


People also ask

How do you access the directive variable in a controller?

You just create a myVar variable in your controller and pass it to the directive using my-var attribute. Since you are using two way binding, any changes made to myVar by the directive are available in your controller.

What is Angular attribute directive?

Angular attribute directives are a number of built-in directives that we can add to our HTML elements that give them a dynamic behavior. In summary, an attribute directive changes the appearance or behavior of a DOM element.


1 Answers

I am using this aproach:

.directive('mydirective',  [function (){     return {       ...       scope: {         id: '@',       },       controller: function ($scope, $element, $attrs) {          $attrs.$observe('id', function(passedId) {            /// do what is needed with passedId          });     ... 

And the directive used and id passed like this:

<div mydirective id="{{someprop.id}}" /> 
like image 186
Radim Köhler Avatar answered Sep 30 '22 17:09

Radim Köhler