如何定义$compile服务何时在angular中完全完成编译

How to define when $compile service completely finish compiling in angular

本文关键字:angular 编译 何时 服务 何定义 定义 compile      更新时间:2023-09-26

假设我有一个在$scope模板中继承的popup指令,它是popup应该显示的popup内容的字符串模板。

scope: {
   template: '=popInfo'//<div another directive></div>
}

这个模板字符串本身可能包含另一个指令,所以我使用$compile服务来编译它

$el.find('content-container').append($compile($scope.template)($scope));
$scope.makeVisible();//after i've compiled i'm making it visible

它有效,但存在看起来像眨眼的副作用。首先是显示弹出式容器,然后是显示内容。

http://plnkr.co/edit/FehvteTvZ92e4MFZNaHj?p=preview-这是一个例子。似乎只使用templateUrl重新部署了它。

有人能帮我避免这种行为吗?

您需要等待一个摘要周期。例如,使元素在$timeout调用中可见:

$el.find('content-container').append($compile($scope.template)($scope));
$timeout($scope.makeVisible);
// or $timeout(function() { $scope.makeVisible(); });