Angular - Change DetectionWhich 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 })Check Answer
Step-by-Step SolutionSolution:Step 1: Recall Angular syntax for change detectionThe correct syntax uses the enum ChangeDetectionStrategy.OnPush inside the component decorator.Step 2: Eliminate incorrect optionsA uses Default strategy, B uses a boolean which is invalid, C uses a string instead of enum.Final Answer:@Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.OnPush }) -> Option BQuick Check:Use enum OnPush in decorator = D [OK]Quick Trick: Use ChangeDetectionStrategy.OnPush enum in @Component [OK]Common Mistakes:MISTAKESUsing string 'OnPush' instead of enumSetting changeDetection to true or falseConfusing Default with OnPush
Master "Change Detection" in Angular9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Angular Quizzes Angular Change Detection - Default change detection strategy - Quiz 8hard HTTP Client - Handling HTTP errors - Quiz 15hard HTTP Client - Loading states and error patterns - Quiz 11easy Routing - RouterLink for navigation - Quiz 15hard Routing - Query parameters and fragments - Quiz 7medium RxJS Operators - combineLatest and forkJoin for combining - Quiz 4medium RxJS Operators - map operator for transformation - Quiz 9hard Services and Dependency Injection - Singleton service behavior - Quiz 5medium Services and Dependency Injection - How dependency injection works in Angular - Quiz 6medium Template-Driven Forms - Two-way binding in forms - Quiz 6medium