WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Angular · Intermediate · question 22 of 100

What are the various change detection strategies in Angular and when to use them?

📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

Change detection is a critical functionality in Angular, responsible for keeping the view in sync with the underlying component data. Angular provides two change detection strategies: Default and OnPush.

1. Default Change Detection Strategy:

The default change detection strategy is the most commonly used strategy in Angular applications. In the default strategy, Angular iteratively checks all components and their bindings for changes in every event. Any changes in the component state trigger the rebuilding of the component tree and update the view accordingly.

Pros:

- Easy to use and works well for simple applications.

- Automatically detects changes and updates the view.

Cons:

- Performance might be slow for large applications with frequent updates, as every event triggers change detection for the whole component tree.

When to use:

- Use this strategy for simple applications or components where the number of bindings and updates is small.

2. OnPush Change Detection Strategy:

The OnPush change detection strategy is an optimization that can significantly improve the performance of Angular applications. This strategy only checks and updates the component tree when one of the following events occurs:

- An input property reference changes.

- An event is triggered from the component or one of its children.

- The developer manually triggers change detection using the ‘markForCheck()‘ or ‘detectChanges()‘ methods.

This means that change detection is not run on every event (like in the default strategy), but only when Angular knows the component might have changed.

Pros:

- Improved performance, especially for large applications with frequent updates.

Cons:

- More complex to use, as changes and updates must be managed properly.

- Requires slightly more cognitive overhead for developers.

When to use:

- Use this strategy for complex applications with many components, bindings, and frequent updating of state.

- Use it for child components where the parent provides the inputs and manages updates for the input properties.

To set the change detection strategy to OnPush for a component, add the following line of code to the component’s decorator:

@Component({
  ...
  changeDetection: ChangeDetectionStrategy.OnPush
})

In summary, the default change detection strategy works well for small applications, but it might lead to performance issues in large applications due to frequent checking and updating. On the other hand, the OnPush strategy is more performant for large applications but requires developers to manage state updates properly. Consider the size and complexity of your application before choosing the appropriate change detection strategy.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Angular interview — then scores it.
📞 Practice Angular — free 15 min
📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

All 100 Angular questions · All topics