NgZone, Angular uses it to profiling all the async actions such as setTimeout, http request and animation.

For example if you dealing with heavy oprations for hundreds of times, you might want it run outside Angular Zone, so it won't trgger change detection hundreds of times.

import { Component, OnInit, DoCheck, NgZone } from '@angular/core';

@Component({
selector: 'app-root',
template: `
<div>
Counter: {{ counter }}
</div>
`
})
export class AppComponent implements OnInit, DoCheck {
counter = ;
constructor(
private zone: NgZone
) {}
ngOnInit() {
this.zone.runOutsideAngular(() => {
for (let i = ; i < ; i++) {
setTimeout(() => this.counter++);
}
this.zone.run(() => {
setTimeout(() => this.counter = this.counter, );
});
});
}
ngDoCheck() {
console.log('Change detection has been run!');
}
}

To notice that, the operation puts inside 'runOutsideAngular' should be async opreation. Otherwise there is no effect. Now you can think that Angular won't update our 'counter' if we run outside Angular.

So if we resume (trigger change detection) again, we can do:

      this.zone.run(() => {
setTimeout(() => this.counter = this.counter, );
});

最新文章

  1. SQLSERVER单表CRUD通用方法
  2. SDL文字和图形
  3. Jquery Mobile中pageinit等函数执行两次的问题【终极解决】
  4. linux防止sshd被爆破(安装denyhosts)
  5. mysql--乱码
  6. notebook kernels
  7. mvn deploy 报错:Return code is: 400, ReasonPhrase: Bad Request. ->
  8. PTA 07-图4 哈利&#183;波特的考试 (25分)
  9. 无线wifi-PJ-之在开启WPS下使用reaver
  10. nginx中配置跨域支持功能
  11. 2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!
  12. hdu 5035 概率论
  13. Source insight 3572安装和版本号An invalid source insight serial number was detected解
  14. SAS随机抽样以及程序初始环境
  15. 海量并发的无锁编程 (lock free programming)
  16. 5步告诉你QQ音乐的完美音质是怎么来的,播放器的秘密都在这里
  17. Ubuntu下在Apache中运行Keystone
  18. python socket 编程之三:长连接、短连接以及心跳(转药师Aric的文章)
  19. HttpClient之EntityUtils对象
  20. eclipse copy web project后修改context root

热门文章

  1. 洛谷 P1850 换教室
  2. sql%rowcount 返回影响行数
  3. ReactNavtive框架教程(4)
  4. php资源类型变量
  5. lightSlider 好图片轮播插件 支持移动端
  6. 2.FastJson公司--阿里巴巴开源的速度最快的Json和对象转换工具
  7. ORA-10458: standby database requires recovery
  8. 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
  9. POJ1308——Is It A Tree?
  10. 7,NULL与nullptr对比