What is the purpose of running code in NgZone.run in angular?

94 views Asked by At

I came across this following code which invokes a dialog inside NgZone.run method. What is the purpose of invoking a dialog inside NgZone.run?

1

There are 1 answers

1
Aviv On

By running a code inside Ngzone.run, it enusures that your code is running inside the angular zone, and any changes to the angular application that triggered by your code (such as data-binding, change-detection, lifecycle hook related changes) are properly detected by angular and affect the application.

During the running of an angular application it might frequently enter and exit the NgZone because of several cases, for example: third-party libraries usage, browser events, http requests...

The rendering of a mat-dialog object can be run outside of NgZone, so you might want to ensure that you are running your code inside the angular zone.