Bird
0
0

Identify the error in this component using OnPush strategy:

medium📝 Debug Q6 of 15
Angular - Performance Optimization
Identify the error in this component using OnPush strategy:
@Component({
  selector: 'app-test',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `{{ title }}`
})
export class TestComponent {
  title = 'Hello';

  updateTitle() {
    this.title = 'World';
  }
}

Why might the template not update after calling updateTitle()?
ABecause the title variable is not decorated with @Input()
BBecause OnPush requires manual change detection calls always
CBecause the method updateTitle() is not called in the template
DBecause OnPush ignores internal property changes without input or event
Step-by-Step Solution
Solution:
  1. Step 1: Understand OnPush and internal state changes

    OnPush does not detect changes from internal property updates unless triggered by input change or event.
  2. Step 2: Apply to updateTitle method

    Changing title internally won't update template unless change detection is triggered.
  3. Final Answer:

    Because OnPush ignores internal property changes without input or event -> Option D
  4. Quick Check:

    OnPush ignores internal changes without triggers [OK]
Quick Trick: Internal changes need input/event to update OnPush component [OK]
Common Mistakes:
  • Thinking @Input() is required for all properties
  • Assuming method call in class updates template automatically
  • Believing OnPush always requires manual detectChanges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes