The constructor runs first to create the component instance.
Then ngOnChanges runs if there are input property changes.
Next ngOnInit runs once after the first ngOnChanges.
Finally, ngAfterViewInit runs after the component's view is fully initialized.
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', standalone: true, template: `Count: {{ count() }}` }) export class CounterComponent { count = signal(0); constructor() { this.count.set(5); } ngOnInit() { this.count.update(c => c + 1); } }
The signal 'count' starts at 0.
In the constructor, it is set to 5.
In ngOnInit, it is updated by adding 1, so 5 + 1 = 6.
import { Component, Input, signal } from '@angular/core'; @Component({ selector: 'app-display', standalone: true, template: `Value: {{ value() }}` }) export class DisplayComponent { value = signal(''); @Input() set data(val: string) { this.value.set(val); } }
The setter runs before the 'value' signal is initialized, so calling set on undefined causes no update.
Afterward, 'value' is initialized to empty string, overwriting the setter's value.
Option C has correct syntax with semicolons and proper lifecycle method order.
Option C misses a semicolon after set(1).
Option C swaps constructor and ngOnInit logic incorrectly.
Option C is similar to D but option C is the cleanest correct syntax.
ngAfterViewInit runs after Angular initializes the component's views and child views.
This is the safe place to access child components or DOM elements.
ngOnInit runs earlier, before the view is fully ready.
constructor runs before anything is set up.
ngAfterContentInit relates to projected content, not child views.