Javascript AngularJS:如何知道$compile何时完成?
在自定义fancybox(又名lightbox,一个对话框)中,我用插值显示内容 在服务中,在“开放”的fancybox方法中,我做到了Javascript AngularJS:如何知道$compile何时完成?,javascript,jquery,angularjs,fancybox,angularjs-scope,Javascript,Jquery,Angularjs,Fancybox,Angularjs Scope,在自定义fancybox(又名lightbox,一个对话框)中,我用插值显示内容 在服务中,在“开放”的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 u
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注入服务,没有什么比单例$scope更好的了 因此,不是
$compile(el)($scope)代码>尝试:
var compiledEl = $compile(el);
....
$compile
返回已编译的数据
作为旁注
我将为指令提供服务,并将其编译成指令。我认为这是正确的方法。我对ngDialog模态和弹出式提供程序也有同样的问题。我需要根据对话框的高度来定位它。但是高度取决于编译的DOM
我最终找到了一个使用$timeout的解决方案,如那篇文章所述:
对于您的代码,它将给出如下内容:
open: function(html, $scope) {
var el = angular.element(html);
$compile(el)($scope);
$timeout(function() {
$.fancybox.open(el);
}, 0);
}
你的枪对我很管用。问题是什么?我从fancybox内部的ng点击获得警报。。。需要更多解释问题是编译结束时更新的变量所产生的闪烁