检测<ng-include src =“...”>加载</ng-include>的时间

时间:2014-08-20 05:34:36

标签: javascript angularjs angularjs-ng-include deferred-rendering

我在模式中使用<ng-include>根据某些选项有条件地加载不同的内容。

当模式以一种模式(heh)或其他模式启动时,或者当用户更改下拉列表时,将选择内容。

在任何这些情况下,我的模板中的一个部分加载到正确的模态内容中:

<!-- Load in the correct modal contents -->
<ng-include src="'modals/contents-'+ form.type +'.html'"></ng-include>

然后加载modals/contents-addressForm.html或任何情况。

诀窍是,不同的形式是不同的大小。当它们加载时,模态需要重新定位。

我在$watch变量上尝试了form.type。这会检测模态启动时的更改和初始值,但重新定位过早发生,因为ng-include尚未加载内容。我尝试过推迟但这并不总是足够长。我可以尝试任意长度的超时,但这似乎是......任意的。

任何知道什么时候加载包含?当下一个ng-include加载?

2 个答案:

答案 0 :(得分:2)

根据the docs,您应该能够收听名为$includeContentLoaded的范围事件来说明这一点。

$scope.$on('$includeContentLoaded', function () {
    // it has loaded!
});

答案 1 :(得分:1)

阅读docs

  

onload(可选)用于评估何时加载新部分的表达式。

<!-- Load in the correct modal contents -->
<ng-include src="'modals/contents-'+ form.type +'.html'"
            onload="myFunction()"></ng-include>