Usage of ngZone.run for new async apis that zone.js hasn't pathced
See original GitHub issueDescription
Is this demo correct? how ngZone knows when to update the view as zone.js hasn’t patch someNewAsyncAPI
export class AppComponent implements OnInit {
constructor(private ngZone: NgZone) {}
ngOnInit() {
// New async API is not handled by Zone, so you need to
// use ngZone.run() to make the asynchronous operation in the Angular zone
// and trigger change detection automatically.
this.ngZone.run(() => {
someNewAsyncAPI(() => {
// update the data of the component
});
});
}
}
What is the affected URL?
https://angular.io/guide/zone#ngzone-run-and-runoutsideofangular
Please provide the steps to reproduce the issue
No response
Please provide the expected behavior vs the actual behavior you encountered
I think the demo code should be updated as below:
export class AppComponent implements OnInit {
constructor(private ngZone: NgZone) {}
ngOnInit() {
// New async API is not handled by Zone, so you need to
// use ngZone.run() to make the asynchronous operation in the Angular zone
// and trigger change detection automatically.
- this.ngZone.run(() => {
someNewAsyncAPI(() => {
+ this.ngZone.run(() => {
// update the data of the component
+ });
});
- });
}
}
Please provide a screenshot if possible
No response
Please provide the exception or error you saw
No response
Is this a browser-specific issue? If so, please specify the device, browser, and version.
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Do you still think that NgZone (zone.js) is required for change ...
Yes, Zone and NgZone is used to automatically trigger change detection as a result of async operations. But since change detection is a...
Read more >NgZone - Angular
A zone is an execution context that persists across async tasks. You can think of it as thread-local storage for the JavaScript VM....
Read more >How Angular uses NgZone/Zone.js for Dirty Checking
In this post, we will be looking in-depth on how Zone.js is used by Angular for dirty checking and running UI updates.
Read more >Angular Change Detection & Zone.js Deep Dive - Beyond Java
Angular uses zone.js to react to every asynchronous API your browser ... All you need to do is to run code that Angular...
Read more >When to use Ngzone.run()? - angular - Stack Overflow
1. @dasfsa, if an event is not in "angular zone", angular can not detect the event. · The example provided in doc does...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Indeed!
I believe you are correct;
NgZone.run
needs to be within the async closure, not around its registration.