何时以及为何在Angular 2中使用“滴答"? [英] When and why to use a 'tick' in Angular 2?

查看:70
本文介绍了何时以及为何在Angular 2中使用“滴答"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到在Angular 2组件类中使用了以下内容:

I have seen the following is used in an Angular 2 component class:

setTimeout(()=>{}, 0);

这意味着,在0秒后将调用一个空函数.我知道这与Javascript事件模型有关,但并不完全了解它.

That means, an empty function is called after 0 seconds. I know this is related to Javascript event model, but do not understand it completely.

请使用一个带有某些代码段的小型真实示例来说明在Angular 2中完成此操作的时间和原因.

Please explain when and why this is done in Angular 2, using a small real world example with some code snippet.

推荐答案

setTimeout(()=>{}, 0);

使Angular对整个应用程序运行更改检测,例如 ApplicationRef.tick

Causes Angular to run change detection for the whole application, like ApplicationRef.tick

zone.js修补异步API(addEventHandlersetTimeout,...),并在回调完成后运行更改检测.

zone.js patches async APIs (addEventHandler, setTimeout, ...) and runs change detection after a callback was completed.

这篇关于何时以及为何在Angular 2中使用“滴答"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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