本文介绍了AngularJS - 内部指令 - postLink功能的调用顺序时,我与templateUrl财产更换模板的属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的局面。
如果我有一个包含2指令外指令

I have a weird situation.If I have an outer directive that contains 2 directives

内指令1 - 有一个模板确定

inner-directive1 - has a template defined.

内指令2 - 有一个 templateUrl 确定

inner-directive2 - has a templateUrl defined.

的内指令1 postLink功能被称为外指令postLink功能之前 - 预期

The inner-directive1 postLink function is being called before the outer-directive postLink function - as expected.

但是,该外层指令postLink后的内指令2 poslink函数被​​调用。 - 未如预期

But, the inner-directive2 poslink function is being called after the outer-directive postLink - NOT as expected.

到postLink函数的调用是:内指令1,外指令,内指令2 的和我所期待的:内指令1,内指令2,外指令

The calls to the postLink function are : inner-directive1, outer-directive, inner-directive2 and I was expecting : inner-directive1, inner-directive2, outer-directive.

对于外部指令的模板:

<div ng-transclude><div inner1></div><div inner2></div></div>

请看的jsfiddle

please look at the JsFiddle

有谁知道原因?
而且是有办法,我可以让它工作,因为它预期?

Does anyone know the reason why?And is there a way I can make it work as it is expected ?

- 请看控制台日志。
谢谢,

JSFiddle - Please look at the console log.Thanks,Ben

推荐答案

这也是为什么,从角指令文档():

Here's why, from the Angular directive docs(http://docs.angularjs.org/guide/directive):

templateUrl - 同模板,但模板是从指定的URL装入。因为模板加载是异步的,编译/链接,直到模板被加载暂停。

这样特殊的指令停止链接到你的模板被加载。在此期间,你的其他指令跳和运行。

So that particular directive stops linking until your template is loaded. During that time your other directives jump in and run.

如果你的时间链接功能是至关重要的,你需要包括模板,而不是直接作为templateUrl。除非有人能想出解决这个一个很酷的方式。

If the timing of you link function is critical, you'll need to include the template directly instead of as a templateUrl. Unless someone can come up with a cool way around this.

这篇关于AngularJS - 内部指令 - postLink功能的调用顺序时,我与templateUrl财产更换模板的属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:43