简体   繁体   中英

AngularJS directive - will the controller always be instantiated before the link function is run?

I have an AngularJS directive. Will the controller always be instantiated before the link function is run?

function MyController() {}

function MyDirective() {
     return {
         scope: {},
         template: template,
         require: 'ngModel',
         restrict: 'E',
         replace: true,
         controller: myController,
         link: function(scope, element, attrs, controller) {}
     };
}

In my tests, the controller is instantiated before the link function is run, but will this always be the case?

Edit: based on this post, it looks like the answer is "yes".

Yes, compile is always executed before link. You are looking for this (official):

https://docs.angularjs.org/guide/compiler

If you want to dig further in this, there is another case to know. Link is divided in pre-link and post-link. The order of execution for those is described very well in this blog post:

http://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM