Javascript 为什么嵌套指令控制器和链接函数的触发顺序取决于模板和模板URL?
我正在使用AngularJs 1.4.8,我注意到了奇怪的行为: 当您有两条指令时,一条指令在另一条指令的内部:Javascript 为什么嵌套指令控制器和链接函数的触发顺序取决于模板和模板URL?,javascript,angularjs,event-handling,Javascript,Angularjs,Event Handling,我正在使用AngularJs 1.4.8,我注意到了奇怪的行为: 当您有两条指令时,一条指令在另一条指令的内部: angular.module('another-module', []) .directive('anotherModuleOuter', function () { return { scope: {}, controller: function
angular.module('another-module', [])
.directive('anotherModuleOuter', function () {
return {
scope: {},
controller: function () {
console.log('another-module-outer - controller');
},
link: function () {
console.log('another-module-outer - link');
},
template: '<div>another-module-outer <div another-module-inner></div></div>'
}
})
.directive('anotherModuleInner', function () {
return {
scope: {},
controller: function () {
console.log('another-module-inner - controller');
},
link: function () {
console.log('another-module-inner - link');
},
template: '<div>another-module-inner</div>'
}
});
这意味着将首先调用外部指令的控制器,然后调用链接函数-last
但是,如果更改Internal的模板:
template: '<div>another-module-inner</div>'
加载顺序也将改变(外部指令的链接将在内部控制器之前触发):
为什么它是这样工作的
附言/
我对内部指令注册$scope.$on()并外部触发$rootScope.$broadcast()的事件有问题,但由于内部函数$on是在广播后注册的,所以它无法捕获任何内容。如果您能提出解决方案,我将不胜感激。您想要的答案在下面的文档中 由于模板加载是异步的,编译器将挂起 编译该元素上的指令,以便稍后在模板 已经解决了。与此同时,它将继续进行编译和编辑 链接同级元素和父元素,就像此元素没有 包含任何指令
谢谢,您对如何解决活动问题有什么建议吗$超时似乎没有帮助,据我所知,我只等待一个摘要周期。您可以在内部触发一个事件,等待它完成后在外部拾取,然后触发它的事件。但是,除非您正在传递数据,否则您可能只需要一个事件。
template: '<div>another-module-inner</div>'
templateUrl: 'anotherModuleInner.html'
another-module-outer - controller
another-module-outer - link
another-module-inner - controller
another-module-inner - link