Solvedangular Async event subscriber not updating UI after async call
✔️Accepted Answer
Hi!
The issue is that the async call result is outside ngZone thus not triggering the UI update.
You can do something like:
public constructor(private _ngZone: NgZone) {
}
public async update() {
try {
this.a = 1;
let newBValue = await this.dataService.getAsync();
this._ngZone.run(() => {
this.b = newBValue;
this.c = 2;
});
}
catch (error) {
// blah
}
}
Other Answers:
Resolved! The culprit was Babel... I'm not sure what precisely, but I reverted all my Babel related npm modules back to a known good version and everything is functioning normally again. Apologies that the title of this issue turned out to be nothing to do with the problem.
I reproduced the issue on the AngularClass Angular2 Webpack Starter project so I can upload that minimal demonstration of the issue if it interests anyone. I simply modified the project to update the UI using setTimeout as above, then modified the Webpack config to use Babel as part of the transpilation process... and suddenly setTimeout stops updating the page.
I have (essentially) the following code in a component of my app:
The TypeScript is being compiled to ES6 then run through Babel for ES5 (as TypeScript can't compile to ES5 with async/await yet).
The UI is displaying variables
a
,b
, andc
. When thechanged
event is raised by thething
service, variablea
is correctly displayed as1
, butb
andc
do not update in the UI. Console logging shows they are updated in the component. If I then trigger an update manually (by clicking some unrelated button that updates the UI),b
andc
are rendered correctly.I've put a breakpoint after the async call, and
Zone.run
is in the call stack.I can try and create a minimal reproduction, but my first question is whether this behaviour is expected (and async event subscribers are simply not supported), or if this looks like a genuine bug.