DongBin Kim
DongBin Kim

Reputation: 1889

Angular change detection: What is outside of Angular(zone)?

It's about change detection in Angular.

They say, "You would want to use detectChanges method if the property of a component changes outside of Angular so the change detection cycle wouldn't be scheduled for that. Otherwise, use markForCheck method."

So at this moment, I'm wondering what case/part of the code would be considered as outside of Angular?

Upvotes: 1

Views: 573

Answers (1)

cgatian
cgatian

Reputation: 22994

The typical application rarely will have events fired that are occuring "outside angular", but they could happen. A good example of this would be a third party control your consuming in your app, such as a image carousel. Since this element on the page was created by a library outside of Angular, Angular has no idea when you want to run change detection. To continue the example, imagine the user clicks a right arrow to advance the slideshow. Since this event was wired up through the non-Angular library, Angular does not know to perform a check on components. This is where the developer needs to then manually run a change detection cycle to see if anything on the component requires updating.

You could also create a component and "detach" it from Angular change detection. Reasons being, the component may fire a large number of events that would typically cause change detection. The classic example is a stock ticker updating every 100ms. If the component wasn't detached, every event would force Angular to perform a change detection cycle. However, if you detach the developer can be strategic and only perform a change detection cycle if the value has actually changed.

Upvotes: 1

Related Questions