Consider an Angular component using the default change detection strategy. What happens when a parent component updates a property that a child component uses?
ParentComponent template: <child-component [data]="parentData"></child-component> ChildComponent: @Input() data: string; // Default change detection strategy // Parent updates parentData on button click
Think about how Angular's default change detection checks all components on every event.
With the default change detection strategy, Angular runs change detection for all components in the component tree whenever an event happens. This means the child component will always check and update its view if needed, even if the input value has not changed.
Given a component with ChangeDetectionStrategy.OnPush, which of the following triggers the component to update?
Component decorator:
@Component({
selector: 'my-comp',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComp {
@Input() data: string;
}OnPush optimizes by checking inputs by reference and internal events.
OnPush strategy tells Angular to check the component only when its input properties change by reference or when an event inside the component triggers change detection. This reduces unnecessary checks and improves performance.
Which code snippet correctly detaches change detection for a component using ChangeDetectorRef?
Look for the official method name to stop change detection.
The detach() method on ChangeDetectorRef stops Angular from running change detection for that component until reattach() is called.
Given a component with ChangeDetectionStrategy.OnPush and an input object, why does updating a nested property not update the view?
Parent template: <child-comp [data]="parentData"></child-comp> Parent component: parentData = { name: 'Alice', age: 30 }; // Later update this.parentData.age = 31;
Think about how OnPush compares input values.
OnPush strategy triggers change detection only when the input reference changes. Mutating a nested property keeps the same object reference, so Angular does not detect the change.
Why does using ChangeDetectionStrategy.OnPush improve performance in large Angular applications?
Consider how Angular decides when to check components with OnPush.
OnPush strategy optimizes performance by limiting change detection to only when input references change or internal events happen, avoiding unnecessary checks of components that have not changed.