根据我在网上读到的资料,角度小组建议你总是在角度区域之外调用requestAnimationFrame(),如下所示:

this.ngZone.runOutsideAngular(() => {
  requestAnimationFrame(timestamp => {
    let timerStart = timestamp || new Date().getTime();
    this.myAnimeMethod(timestamp);
  });
});

但是循环呢…
this.ngZone.runOutsideAngular(() => {
  requestAnimationFrame(timestamp => {
    let timerStart = timestamp;
    this.myAnimeMethod(timestamp, timerStart);
  });
});

myAnimeMethod(timestamp, timerStart) {
  let time = timestamp || new Date().getTime();
  let runtime = time - timerStart;

  /// animation logic here

  if(runtime < 10000) {

    // ------- continue to animate for 10 seconds -- //

    requestAnimationFrame(timestamp => {
      this.myAnimeMethod(timestamp, timerStart);
    });
  }
}

在第一个请求时调用this.ngZone.runOutsideAngular()就足够了吗?还是应该在this.ngZone.runOutsideAngular()内部再次调用它?
this.ngZone.runOutsideAngular(() => {
  requestAnimationFrame(timestamp => {
    let timerStart = timestamp;
    this.myAnimeMethod(timestamp, timerStart);
  });
});

myAnimeMethod(timestamp, timerStart) {
  let time = timestamp || new Date().getTime();
  let runtime = time - timerStart;

  /// animation logic here

  if(runtime < 10000) {

    // ------- request to run outside of Angular again while continuing to animate for 10 seconds -- //

    this.ngZone.runOutsideAngular(() => {
      requestAnimationFrame(timestamp => {
        this.myAnimeMethod(timestamp, timerStart);
      });
    });

  }
}

最佳答案

简而言之:您不需要继续从NgZone.runOutsideAngular处理程序调用requestAnimationFrame,该处理程序是从角度外调用的。
长话短说:一旦你进入“根”区域(这是你调用NgZone.runOutsideAngular时得到的),任何requestAnimationFrame回调也将从该区域运行,除非你明确请求一个不同的区域,例如viaNgZone.run
要检查这一点,请尝试从您的NgZone.isInAngularZone()处理程序调用静态函数requestAnimationFrame
注意,我用Angular 4.4.4和Zone.js 0.8.18测试了这个。

关于angular - Angular 2,将ngZone.runOutsideAngular与requestAnimationFrame循环一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43913277/

10-10 15:20