Angular 2 - Why do I need zone.run()?

后端 未结 2 840
有刺的猬
有刺的猬 2020-12-17 18:16

I am trying to create a component in Angular 2 which displays data from a service. The service basically loads some data from a json file after some input from the user. I\'

相关标签:
2条回答
  • 2020-12-17 18:51

    Is it possibly caused by referencing assetService, the argument, and not this.assetService, in your views? Maybe that is causing Angular's change detection not to be triggered without calling zone.run().

    0 讨论(0)
  • 2020-12-17 19:04

    This would require knowledge about the inner workings of the AssetService you're using.

    Angular runs the code of your components within its zone where most async APIs (addEventListener, setTimeout, ...) are patched so the zone can notify Angular when such an async callback has happend. This is when Angular runs change detection.

    If you initialized AssetService outside Angular or AssetService by other means executes code outside Angulars zone, then Angular doesn't get notified about happened async callbacks and doesn't run change detection.

    With zone.run(...) you explicitely make code execute inside Angulars zone and change detection is run afterwards.

    0 讨论(0)
提交回复
热议问题