javascript - 嵌套的 Angular 指令在 `template` 和 `templateUrl` 上表现不同

标签 javascript angularjs angularjs-directive

我有一个“助手”指令,它允许我轻松访问父指令中的子 DOM 元素。但是,当我将此辅助指令与另一个指令结合使用时遇到了问题。如果我在指令中使用 template,一切正常。但是,如果我使用 templateUrl,我会收到一条错误消息,指出 helper 指令保存到范围的元素未定义。

有点难以解释,但这里是 plunkr: http://plnkr.co/edit/NHKBGfro7Xe3dSalq5bt?p=preview

注意:我添加了一个在 5 秒后触发的 setTimeout,它允许 templateUrl 指令工作,因为它现在在父指令的范围内可用。

最佳答案

这是预期的行为。

angularjs $compile

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.

Related Github Issue

关于javascript - 嵌套的 Angular 指令在 `template` 和 `templateUrl` 上表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393070/

相关文章:

jquery - AngularUI Modal - 防止 body 移动和滚动

angularjs - Angular.js : Wrapping elements in a nested transclude

javascript - HTML "Range"输入问题

javascript - 使用地理位置获取城市名称

javascript - 使用 ng-view 时可以保留变量作为 View 之间的引用吗?

angularjs - Angular JS触摸和滑动

javascript - Node - 访问多维数组的最后一个索引

javascript - Ext js 使用另一个日期选择器的值处理设置日期选择器的 minDate

angularjs-directive - 如何防止 AngularJS 路由使用 ~(代字号)编码 url 参数

javascript - "require"Angular 指令的 DDO 选项在应该抛出错误时没有抛出错误