Skip to content Skip to sidebar Skip to footer

How To Make A Directive Link Function To Run Only Once

I have a directive in angular: function myDirective() { return { scope: {}, restrict: 'A', link: function($scope, element) { console.warn('my

Solution 1:

This is expected behavior. Every time your ng-if evaluates to true, it will render this directive (more specifically, a new instance of this directive). It sounds like you need to control this with your ng-if.

You example:

<divmy-directiveshouldRun="true"></div>

Instead, use logic in your controller to conditionally render with

<divmy-directiveng-if="..."></div>

Since you are having trouble with ng-if, you probably need to add other conditions like

Note that whenever ng-if evaluates to false and then to true again, your directive will be rerendered. This is logic that should be in your controller.

Post a Comment for "How To Make A Directive Link Function To Run Only Once"