question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage of ngZone.run for new async apis that zone.js hasn't pathced

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Feb 11, 2022

Indeed!

1reaction
JoostKcommented, Jan 14, 2022

I believe you are correct; NgZone.run needs to be within the async closure, not around its registration.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found