4

Many of my directive (soon to become components) takes their scope from variables set by other directives. Currently in each directive I have to watch my scope to know if it has changed which seems to complicate the code unnecessarily. So I started using ng-if="vm.ready" on my tag to reinstantiate the directive when I need it to. But then the management of that state is left outside of the directive which is harder to maintain.

I am wondering if angular provide such a mechanism when if the scope of your directive change then it will at least reinstantiate your directive controller.

Thanks

Pascal DeMilly
  • 681
  • 1
  • 6
  • 16

1 Answers1

1

You can probably use $onInit()

After the controller is instantiated, the initial values of the isolate scope bindings will be bound to the controller properties. You can access these bindings once they have been initialized by providing a controller method called $onInit, which is called after all the controllers on an element have been constructed and had their bindings initialized.

https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L250

malix
  • 3,566
  • 1
  • 31
  • 41
  • 1
    Thanks was just reading Motto tweet about angular 1.5 component and he talk about $onInit and $onChange which as you suggested what I am looking for. Upgrading to 1.5 now. I guess that works only in components not directives so I have a few to port. Doesn't look too difficult. thanks [https://toddmotto.com/angular-1-5-lifecycle-hooks] – Pascal DeMilly Jun 05 '16 at 03:21