Bird
0
0

Which of the following is the correct way to set OnPush change detection in an Angular component?

easy📝 Syntax Q12 of 15
Angular - Change Detection
Which of the following is the correct way to set OnPush change detection in an Angular component?
A@Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.Default })
B@Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.OnPush })
C@Component({ selector: 'app-example', changeDetection: 'OnPush' })
D@Component({ selector: 'app-example', changeDetection: true })
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular syntax for change detection

    The correct syntax uses the enum ChangeDetectionStrategy.OnPush inside the component decorator.
  2. Step 2: Eliminate incorrect options

    A uses Default strategy, B uses a boolean which is invalid, C uses a string instead of enum.
  3. Final Answer:

    @Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.OnPush }) -> Option B
  4. Quick Check:

    Use enum OnPush in decorator = D [OK]
Quick Trick: Use ChangeDetectionStrategy.OnPush enum in @Component [OK]
Common Mistakes:
MISTAKES
  • Using string 'OnPush' instead of enum
  • Setting changeDetection to true or false
  • Confusing Default with OnPush

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes