我有一个“助手”指令,它允许我轻松访问父指令中的子 DOM 元素。但是,当我将此辅助指令与另一个指令结合使用时遇到了问题。如果我在指令中使用 template
,一切正常。但是,如果我使用 templateUrl
,我会收到一条错误消息,指出 helper 指令保存到范围的元素未定义。
有点难以解释,但这里是 plunkr: http://plnkr.co/edit/NHKBGfro7Xe3dSalq5bt?p=preview
注意:我添加了一个在 5 秒后触发的 setTimeout,它允许 templateUrl
指令工作,因为它现在在父指令的范围内可用。
最佳答案
这是预期的行为。
Post-linking function Executed after the child elements are linked.
Note that child elements that contain templateUrl directives will not have been compiled and linked since they are waiting for their template to load asynchronously and their own compilation and linking has been suspended until that occurs.
It is safe to do DOM transformation in the post-linking function on elements that are not waiting for their async templates to be resolved.
关于javascript - 嵌套的 Angular 指令在 `template` 和 `templateUrl` 上表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393070/