AngularJS:火灾后$范围立即事件$消化。 [英] AngularJS: Fire an event immediately after $scope.$digest
问题描述
在我的AngularJS应用程序,有一个在我想等待一个$范围被加工成DOM,然后在其上运行一些code,就像一个jQuery淡入,例如几个百分点。
In my AngularJS app, there's several points at which I want to wait for a $scope to be processed into the DOM, and then run some code on it, like a jquery fadeIn, for example.
有没有听某种形式的digestComplete消息的方式吗?
Is there a way to listen for a digestComplete message of some sort?
我目前的方法是,设定什么$范围变量我想渲染后,立即使用的setTimeout 0毫秒的延迟,因此,它可以让范围内完成消化,然后运行code,它完美的作品。唯一的问题是,我很偶然看到的setTimeout返回之前的DOM渲染。我想这是保证摘要火后,并呈现前一种方法。任何想法?
My current method is, immediately after setting whatever $scope variables I want rendered, use setTimeout with a delay of 0 ms, so that it will let the scope finish digesting, and then run the code, which works perfectly. Only problem is, I very occasionally see the DOM render before that setTimeout returns. I'd like a method that is guaranteed to fire after digest, and before render. Any ideas?
推荐答案
在此 jQuery的淡入和出小提琴(我发现它 wiki页面的 JSFiddles例子),笔者定义在指令中的的链接fadey指令并执行jQuery的淡入(或淡出)的功能
<li ng-repeat="item in items" fadey="500">
...
myApp.directive('fadey', function() {
return {
restrict: 'A',
link: function(scope, elm, attrs) {
var duration = parseInt(attrs.fadey);
if (isNaN(duration)) {
duration = 500;
}
elm = jQuery(elm); // this line is not needed if jQuery is loaded before Angular
elm.hide();
elm.fadeIn(duration)
另一种可能的解决方案是使用 $ evalAsync :看的this评论通过MISKO,他在其中指出:
Another possible solution is to use $evalAsync: see this comment by Miško, in which he states:
该asyncEval是DOM施工后,但在浏览器呈现前。
我相信这是你要附加的jQuery插件的时间。除此以外
你会有闪烁。如果你真的想要做的浏览器渲染后
你可以做$延迟(FN,0);
The asyncEval is after the DOM construction but before the browser renders. I believe that is the time you want to attach the jquery plugins. otherwise you will have flicker. if you really want to do after the browser render you can do $defer(fn, 0);
($延迟更名为$超时)。
($defer was renamed $timeout).
不过,我觉得用指令(因为你操纵DOM)是更好的方法。
However, I think using a directive (since you are manipulating the DOM) is the better approach.
下面是一个<一个href=\"http://stackoverflow.com/questions/12021152/jquery-calls-not-working-in-viewcontentloaded-of-angular\">SO张贴其中OP尝试监听上的范围$ viewContentLoaded事件(这是又一替代),以适用于一些jQuery函数。建议/答案是再次使用指令。
Here's a SO post where the OP tried listening for $viewContentLoaded events on the scope (which is yet another alternative), in order to apply some jQuery functions. The suggestion/answer was again to use a directive.
这篇关于AngularJS:火灾后$范围立即事件$消化。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!