我有一个带有 ngCloak 指令的 Angular 网页。它使用pym.js加载到动态大小的 iframe 中。
问题是除非我调整浏览器大小或触发调整大小事件,或者在页面加载后调用 pymChild.sendHeight() ,否则页面不会出现。
我没有看到任何与 ngCloak 相关的事件。是否存在“页面已呈现,控制器已初始化”的角度事件?
有 $timeout 服务:
$timeout(function() {
// this code will execute after the render phase
});
您可以编写一个在函数中执行回调的指令postLink
,因为 postLink 将在$compile
生命周期的最后被调用。
.directive('onInitialized', function ($parse) {
return {
restrict: 'A',
priority: 1000, // to ensure that the postLink run last.
link: function postLink(scope, element, attrs) {
$parse(attrs.onInitialized)(scope);
}
}
});
并将其放置在您想知道它及其所有后代何时template-ready
编译的元素上,例如:
<body ng-controller="MainCtrl" on-initialized="hello()">
在MainCtrl
控制器中:
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.hello = function () {
console.log('Hello ' + $scope.name);
};
})
对于template-ready
,我的意思是所有指令,除了: 指令templateUrl
和模板还没有在 $templateCache 中准备好,因为它们将被异步编译。
希望这可以帮助。