The default change detection strategy in Angular automatically updates the view whenever data changes. It helps keep the screen in sync with the app's data without extra work.
Default change detection strategy in Angular
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>{{ message }}</p>`, changeDetection: ChangeDetectionStrategy.Default }) export class ExampleComponent { message = 'Hello!'; }
The default strategy is ChangeDetectionStrategy.Default.
You do not have to set it explicitly because Angular uses it by default.
changeDetection: ChangeDetectionStrategy.Default
changeDetection, Angular uses the default strategy automatically.@Component({
selector: 'app-sample',
template: `<div>{{ count }}</div>`
})
export class SampleComponent {
count = 0;
}This component shows a number and a button. When you click the button, the number increases. The default change detection updates the view automatically to show the new number.
import { Component } from '@angular/core'; @Component({ selector: 'app-counter', template: ` <p>Count: {{ count }}</p> <button (click)="increment()">Increase</button> ` }) export class CounterComponent { count = 0; increment() { this.count++; } }
The default strategy checks every component on each event, which is simple but can be slower for big apps.
You can improve performance by using ChangeDetectionStrategy.OnPush in advanced cases.
The default change detection strategy updates the view automatically when data changes.
It works well for most apps and is easy to use.
For better performance in big apps, consider other strategies.
Practice
Solution
Step 1: Understand default change detection behavior
The default strategy automatically checks for data changes and updates the view accordingly.Step 2: Compare with other options
Other options require manual updates or disable detection, which is not the default behavior.Final Answer:
Automatically updates the view when data changes -> Option BQuick Check:
Default strategy = automatic update [OK]
- Thinking manual update is default
- Confusing default with OnPush strategy
- Believing change detection is disabled by default
Solution
Step 1: Recall Angular syntax for change detection
The changeDetection property expects a value from the ChangeDetectionStrategy enum, not a string.Step 2: Identify correct enum usage
ChangeDetectionStrategy.Default is the correct enum value for default strategy.Final Answer:
@Component({ changeDetection: ChangeDetectionStrategy.Default }) -> Option CQuick Check:
Use enum, not string, for changeDetection [OK]
- Using strings instead of enum values
- Confusing 'default' with 'Default' string
- Mixing OnPush with default strategy
@Component({
selector: 'app-counter',
template: `Count: {{count}}`
})
export class CounterComponent {
count = 0;
increment() {
this.count++;
}
}What will happen when the button is clicked?
Solution
Step 1: Understand default change detection behavior on event
Default strategy runs change detection automatically after events like button clicks.Step 2: Predict view update
When increment() updates count, the view updates automatically to show the new count.Final Answer:
The displayed count will increase by 1 automatically -> Option DQuick Check:
Default strategy updates view on event [OK]
- Thinking manual refresh is needed
- Assuming change detection is off
- Believing count changes but view stays same
@Component({
selector: 'app-sample',
template: `{{value}}`
})
export class SampleComponent {
value = 10;
updateValue() {
this.value = 20;
}
}After calling
updateValue(), the view does not update. What is the most likely cause?Solution
Step 1: Check default strategy behavior
Default strategy updates view automatically unless changes happen outside Angular zone.Step 2: Identify cause of no update
If updateValue() runs outside Angular zone, change detection won't trigger, so view stays old.Final Answer:
The updateValue method is not called inside Angular zone -> Option AQuick Check:
Outside Angular zone blocks default detection [OK]
- Assuming default strategy is disabled
- Confusing OnPush input change with default strategy
- Thinking private properties cause no update
Solution
Step 1: Understand default strategy limits
Default strategy runs change detection frequently, which can be costly in large apps.Step 2: Identify how to limit detection on one component
Using ChangeDetectorRef.detach() stops change detection on that component until manually reattached.Step 3: Evaluate other options
Setting default again does nothing new; markForCheck forces detection; OnPush changes strategy globally, not selectively.Final Answer:
Use ChangeDetectorRef.detach() to stop change detection on that component manually -> Option AQuick Check:
Detach stops detection selectively [OK]
- Thinking setting default again limits detection
- Confusing markForCheck with stopping detection
- Assuming OnPush is selective per component
Ready to prove it?
Pass the test and this pattern moves unknown
