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.
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.
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.
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}}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With