angularjs – 当范围改变时,angular指令自动重新实例化

我的许多指令(很快将成为组件)从其他指令设置的变量中获取其范围.目前在每个指令中,我必须观察我的范围,以了解它是否已经改变,这似乎使代码不必要地复杂化.所以我开始在我的标签上使用ng-if =“vm.ready”来在需要时重新实现指令.但是那个国家的管理权被排除在指令之外,而这个指令更难以维持.

我想知道如果您的指令的范围发生变化,那么angular是否提供了这样的机制,那么它至少会重新实现您的指令控制器.

谢谢

最佳答案 你可以使用$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

点赞