Complete the code to set the change detection strategy to OnPush in an Angular component.
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-sample', template: `<p>Hello OnPush!</p>`, changeDetection: ChangeDetectionStrategy.[1] }) export class SampleComponent {}
The ChangeDetectionStrategy.OnPush tells Angular to check the component only when its inputs change or an event occurs inside it.
Complete the code to import the correct Angular symbol needed to use OnPush strategy.
import { Component, [1] } from '@angular/core';
You need to import ChangeDetectionStrategy to set the OnPush strategy in the component decorator.
Fix the error in the component decorator to correctly use OnPush change detection.
@Component({
selector: 'app-test',
template: `<p>Test</p>`,
changeDetection: [1]
})
export class TestComponent {}The correct syntax is ChangeDetectionStrategy.OnPush with exact casing and enum name.
Fill both blanks to create a component with OnPush strategy and a simple input property.
import { Component, Input, [1] } from '@angular/core'; @Component({ selector: 'app-item', template: `<p>{{data}}</p>`, changeDetection: [2] }) export class ItemComponent { @Input() data!: string; }
You import ChangeDetectionStrategy and set changeDetection to ChangeDetectionStrategy.OnPush to enable OnPush strategy.
Fill all three blanks to create a component with OnPush strategy, an input property, and a method that triggers change detection manually.
import { Component, Input, [1], [2] } from '@angular/core'; @Component({ selector: 'app-detail', template: `<p>{{info}}</p>`, changeDetection: [3] }) export class DetailComponent { @Input() info!: string; constructor(private cd: [2]) {} refresh() { this.cd.detectChanges(); } }
You import ChangeDetectionStrategy and ChangeDetectorRef. The decorator uses ChangeDetectionStrategy.OnPush. The constructor injects ChangeDetectorRef to manually trigger change detection.