Full answer
The constructor belongs to JavaScript and TypeScript. It runs when Angular creates the component instance. At that moment Angular has injected the dependencies, but it has not yet set the input values, and the template has not been rendered.
ngOnInit is a lifecycle hook that Angular calls once, after it has set the initial input values and before the first rendering of the template. It is the place for initialisation that depends on inputs or needs to start work, such as loading data for an id input.
Guidelines:
- Constructor: receive injected services (or use
inject()), set up simple fields, create effects andtoSignalvalues that need an injection context. Keep it fast and free of side effects such as HTTP calls. ngOnInit: use input values, call services to load data, subscribe to streams that depend on inputs.ngOnDestroy(orDestroyRef): clean up.
Why not do everything in the constructor?
- Inputs are not set yet, so a classic
@Inputisundefined, and reading a required signal input throws (the compiler even reports this as error NG8118). - Testing is harder, because creating the class starts work.
- Angular promises the hook order. The constructor order is just object creation.
With signal inputs the picture changes a little: input() reads are reactive, so you can use computed, effect, or resource with the input and do not need ngOnInit for many cases. For example resource({ params: () => this.id(), loader }) reloads automatically when the input changes.
ngOnInit runs only once. If the input changes later, use ngOnChanges, a computed, or an effect.
import { Component, Input, OnInit, inject, input } from '@angular/core';
export class MarksService {
load(id: number): string {
return `marks for student ${id}`;
}
}
@Component({
selector: 'app-student-marks',
providers: [MarksService],
template: `<p>{{ text }}</p>`,
})
export class StudentMarksComponent implements OnInit {
private readonly service = inject(MarksService);
readonly id = input.required<number>(); // signal input
@Input() legacyId?: number; // classic input
text = '';
legacyInConstructor: number | undefined;
constructor() {
this.legacyInConstructor = this.legacyId; // undefined: inputs are not set yet
// this.id() here would be rejected by the compiler (NG8118).
}
ngOnInit(): void {
this.text = this.service.load(this.id()) + ' / legacy ' + this.legacyId;
}
}Angular 20.3, strict template check passes. A Karma test checks that the classic input is undefined in the constructor and that ngOnInit sees the signal input value. The compiler rejects reading the required signal input in the constructor, as the text says.
Key points
- The constructor runs at creation, with dependencies but without input values.
- `ngOnInit` runs once after the first inputs are set.
- Keep constructors light. Start loading in `ngOnInit` or with signals and `resource`.
- Signal inputs reduce the need for `ngOnInit`.
Common mistakes
- Reading an `@Input` in the constructor and getting `undefined`.
- Making HTTP calls in the constructor, which makes tests and creation heavy.
- Expecting `ngOnInit` to run again when an input changes.
Follow-ups
- When would you use `ngOnChanges` instead?
- How does `inject()` compare with constructor parameters?
- How would a `resource` replace `ngOnInit` loading code?
Tips
- State clearly what is available in each (dependencies versus inputs).
- Mention signal inputs to show current knowledge.
- Give the "load by id" example.
Evaluation rubric
- Explains the difference in timing and responsibility.
- Gives practical guidance on what goes where.
- Knows `ngOnInit` runs once.