我已经浏览过这个链接 https://www.undefinednull.com/2014/07/07/practical-guide-to-prelink-postlink-and-controller-methods-of-angular-directives/ ,他们说链接和 Controller 的执行顺序(从头到尾)是
但在这里我读到 AngularJS: What is the need of the directive's link function when we already had directive's controller with scope?链接在 Controller 之前执行。我应该相信哪一个?
最佳答案
如果是第一个 link
然后 controller
不可能 require
其他指令,并在 link
中使用他们的 Controller 功能。
看一下来自 documentation 的代码:
var directiveDefinitionObject = {
controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... },
controllerAs: 'stringIdentifier',
require: 'siblingDirectiveName', // requiring another directive
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... }, //siblingDirectiveName's controller is available in link function
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
},
};
return directiveDefinitionObject;
});
为了支持这一声明,我们可以阅读同一页:controller
Controller constructor function. The controller is instantiated before the pre-linking phase and can be accessed by other directives (see require attribute). This allows the directives to communicate with each other and augment each other's behavior.
关于angularjs - 在 angularjs 指令中哪个函数将首先执行?链接或 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41162043/