触发Angular2变化检测手动 [英] Triggering Angular2 change detection manually

查看:1629
本文介绍了触发Angular2变化检测手动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写有一个属性的角2分量模式():字符串。我希望能够以编程不响应任何事件设置该属性。问题是,在不存在一个浏览器事件,模板结合 {{模式}} 不更新。是否有手动触发这个变化检测的方法吗?

I'm writing an Angular 2 component that has a property Mode(): string. I would like to be able to set this property programmatically not in response to any event. The problem is that in the absence of a browser event, a template binding {{Mode}} doesn't update. Is there a way to trigger this change detection manually?

推荐答案

尝试其中之一:


  • ApplicationRef.tick() - 类似于角1的 $ rootScope $摘要() - 即,检查全组件树

  • NgZone.run(回调) - 类似于 $ rootScope $申请(回调) - 也就是说,评价角度2区域内的回调函数。我想,但我不知道,这最终执行回调函数后检查全组件树。

  • ChangeDetectorRef.detectChanges() - 类似于 $ $范围摘要() - 即只检查该组件及其子

  • ApplicationRef.tick() - similar to Angular 1's $rootScope.$digest() -- i.e., check the full component tree
  • NgZone.run(callback) - similar to $rootScope.$apply(callback) -- i.e., evaluate the callback function inside the Angular 2 zone. I think, but I'm not sure, that this ends up checking the full component tree after executing the callback function.
  • ChangeDetectorRef.detectChanges() - similar to $scope.$digest() -- i.e., check only this component and its children

您可以注入 ApplicationRef NgZone ChangeDetectorRef 到您的组件。

You can inject ApplicationRef, NgZone, or ChangeDetectorRef into your component.

这篇关于触发Angular2变化检测手动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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