gpt4 book ai didi

javascript - AngularJS:如何知道 $compile 何时完成?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:02 24 4
gpt4 key购买 nike

http://plnkr.co/edit/GRVZl35D1cuWz1kzXZfF?p=preview

在自定义的 fancybox(又名灯箱,一个对话框)中,我显示带有内插值的内容。

在服务中,在“打开”fancybox 方法中,我这样做

 open: function(html, $scope) {
var el = angular.element(html);
$compile(el)($scope); // how to know when the $compile is over?
$.fancybox.open(el); // the uncompiled is shown before the compiled
}

问题是对话框中的内容是在 $compile 结束之前加载的,所以不到一秒钟后我就用这些值刷新了对话框内容。

plunkr 可以工作,但我想避免在它完全编译之前显示“el”:我只想在 $compile 完成他的工作后显示它

有没有办法知道 $compile 什么时候结束,然后我才会在 fancybox 上显示内容?

最佳答案

你不能将 $scope 注入(inject)到服务中,没有什么比单例 $scope 更好的了。

所以不是 $compile(el)($scope); 尝试:

var compiledEl = $compile(el);
....

$compile 返回编译数据。

作为旁注

我会为指令提供服务并将其编译成指令。我认为这是正确的方法。

关于javascript - AngularJS:如何知道 $compile 何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com