javascript - 检测 <ng-include src ="..."> 何时加载

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

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

当模式以一种模式(嘿)或另一种模式启动时,或者当用户更改下拉菜单时,内容被选中。

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

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

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

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

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

有什么方法可以知道什么时候加载 ng-include?下一个 ng-include 何时加载?

最佳答案

根据 the docs ,您应该能够监听一个名为 $includeContentLoaded 的范围事件来告知这一点。

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

关于javascript - 检测 <ng-include src ="..."> 何时加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397348/

相关文章:

javascript - 如何找到特定键的键码

javascript - DIV不隐藏时如何切换图像

angularjs - Angular UI - Bootstrap Accordion 不工作/动态 ng-include

javascript - ng-repeat 与动态 ng-include 和模板变量范围问题

angularjs - 带有 TypeScript 2.x、@types 和 SystemJS 的 Angular 1.x - 使用全局类型

html - ng-view 内部的 ng-include 出现 404 问题

javascript - Google map - 删除以前的标记

javascript - 更改事件输入框上的标签颜色

javascript - ng-include 没有获取带有 utf-8 字符的文件名

javascript - 如何使用 AngularJS 删除输入字段的最后一个字符