Problem Statement
When would you use NgZone or runOutsideAngular?
Explanation
Angular’s zone triggers change detection for async tasks. For heavy, non-UI work (timers, large loops, web workers) use `runOutsideAngular` to prevent excessive checks; when finished, call `run` to update the view. This keeps the UI responsive on CPU-intensive operations.
Code Solution
SolutionRead Only
constructor(private zone: NgZone){}
startHeavyWork(){
this.zone.runOutsideAngular(() => {
// expensive work
doExpensiveStuff();
this.zone.run(() => this.progress = 100); // re-enter to update UI
});
}