如何检查消化周期已经稳定(又名"有角成品编译") [英] How to check if the digest cycles have stabilized (aka "Has angular finished compilation?")

查看:179
本文介绍了如何检查消化周期已经稳定(又名"有角成品编译")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;博士:最初的问题是如何触发回调每个周期消化?但潜在的问题是更有趣,因为这两种回答,我继续修改标题。 =)

上下文:我试图来控制角时,编译完成后的HTML(搜索引擎优化prerendering原因),解决所有依赖,ngincludes,API调用等后
在最聪明的方式到目前为止,我所发现的是通过检查是否消化周期也趋于稳定。照片所以我想,如果我每次消化周期运行的回调被触发并保持到当前的时间,如果没有其他周期是任意的推移(2000毫秒)内触发,我们可以认为编译稳定了,页面已准备好存档SEO爬虫。

迄今取得的进展:我想通看$ rootScope $$阶段会做,但同时大量的互动应该触发该观察者,我发现它只是触发一次,在第一个负载。

下面是我的code:

app.run(函数($ rootScope){
  VAR lastTimeout;
  VAR关闭= $ rootScope。$腕表('$$阶段,功能(newPhase){
    如果(newPhase){
      如果(lastTimeout){
        clearTimeout(lastTimeout);
      }
      lastTimeout = setTimeout的(函数(){
        警报('页稳定!');
      },2000);
    }
  });

结果


解决方案: Mr_Mig 的解决方案(荣誉),加上一些改进。

  app.run(函数($ rootScope){
  VAR lastTimeout;
  VAR关闭= $ rootScope。$表(函数(){
    如果(lastTimeout){
      clearTimeout(lastTimeout);
    }
    lastTimeout = setTimeout的(函数(){
      关闭(); //如果你想跟踪每一个消化稳定评论
      //自定义逻辑
    },2000);
  });
});


解决方案

其实我不知道,如果我的建议会回答你的问题,但你可以简单地传递一个监听器 $观看函数将在每次迭代被称为:

  $ rootScope。$腕表(功能(OLDVAL,的newval){
    //这里补充一些逻辑,将在每个周期消化被称为
});

看看这里: HTTP://docs.angularjs .ORG / API / NG /类型/ $ rootScope.Scope#$看

tl;dr: The initial question was "How to trigger a callback every digest cycle?" but the underlying question is much more interesting and since this answers both, I went ahead and modified the title. =)

Context: I'm trying to control when angular has finished compiling the HTML (for SEO prerendering reasons), after resolving all of its dependencies, ngincludes, API calls, etc. The "smartest" way I have found so far is via checking whether digest cycles have stabilized.
So I figured that if I run a callback each time a digest cycle is triggered and hold on to the current time, if no other cycle is triggered within an arbitrary lapse (2000ms), we can consider that the compilation has stabilized and the page is ready to be archived for SEO crawlers.

Progress so far: I figured watching $rootScope.$$phase would do but, while lots of interactions should trigger that watcher, I'm finding it only triggers once, at the very first load.

Here's my code:

app.run(function ($rootScope) {
  var lastTimeout;
  var off = $rootScope.$watch('$$phase', function (newPhase) {
    if (newPhase) {
      if (lastTimeout) {
        clearTimeout(lastTimeout);
      }
      lastTimeout = setTimeout(function () {
        alert('Page stabilized!');
      }, 2000);
    }
  });



Solution: Added Mr_Mig's solution (kudos) plus some improvements.

app.run(function ($rootScope) {
  var lastTimeout;
  var off = $rootScope.$watch(function () {
    if (lastTimeout) {
      clearTimeout(lastTimeout);
    }
    lastTimeout = setTimeout(function() {
      off(); // comment if you want to track every digest stabilization
      // custom logic
    }, 2000);
  });
});

解决方案

I actually do not know if my advice will answer your question, but you could simply pass a listener to the $watch function which will be called on each iteration:

$rootScope.$watch(function(oldVal, newVal){
    // add some logic here which will be called on each digest cycle
});

Have a look here: http://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch

这篇关于如何检查消化周期已经稳定(又名"有角成品编译")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆