Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reinitialize an Angular directive?

I'm using the fcsaNumber directive to check for the validity of numbers entered into an input box. I got this working, but the value im checking for is dynamic, meaning it can change depending upon what options have been selected. It appears this directive only initializes once. what do I need to do to get it to reinitialize when the options have changed?

//this works fine
    <input name="Employees" type="text" ng-model="Input.Employees" fcsa-number="{min: 1}" required="">

//this only inits once, so its not dynamic
    <input name="Employees" type="text" ng-model="Input.Employees" fcsa-number="{{getMin()}}" required="">
like image 918
Mr Smith Avatar asked Mar 28 '16 14:03

Mr Smith


People also ask

Which directive is used to completely remove and recreate an element in Angular?

Add or remove an element by applying an NgIf directive to a host element. When NgIf is false , Angular removes an element and its descendants from the DOM.

Can a directive have a template in Angular?

Components are directives with templates. The only difference between Components and the other two types of directives is the Template. Attribute and Structural Directives don't have Templates. So, we can say that the Component is a cleaner version of the Directive with a template, which is easier to use.

Which of the directive is used to initialize AngularJS?

The ng-init directive is used to initialize an AngularJS Application data.


2 Answers

Rebuilding the whole directive would work, but it's a brute-force solution -- and particularly undesirable for directives like this one that accept user input, which would get blown away were you to re-init the directive.

Instead you can use scope.$watch or attrs.$observe to allow the directive to more readily respond to changes in its attribute values:

module.directive('foo', function() {
    return {
        scope: {
            'bar': '@'
        },
        link: function(scope, element, attrs) {
            scope.$watch('bar', function() {
                // runs when scoped value 'bar' changes
            });
            attrs.$observe('baz', function() {
                // Similar to $watch, but without isolate scope
            });
        }
    }
});

AngularJS : Difference between the $observe and $watch methods has a good explanation of the differences between $observe and $watch.

like image 63
Daniel Beck Avatar answered Oct 03 '22 20:10

Daniel Beck


You could potentially use ng-if to initialize / tear down the directive in question. (ng-if docs)

The ngIf directive removes or recreates a portion of the DOM tree based on an expression. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM - CodeHater

Here's a great stack overflow answer on the differences between ng-if and ng-show.

like image 29
Alex Johnson Avatar answered Oct 03 '22 22:10

Alex Johnson