Sub topic 11
When input values are available
This chapter assumes you can declare input() and input.required(), read signals by calling them, and use computed() and effect(). It covers when a child component can safely read its inputs.
Inputs are not set at construction
Angular creates a component by calling its constructor. It writes the parent's bound values into the inputs afterward. So during construction, an input still holds its initial state.
- An optional input returns its default value.
- A required input has no value yet. Reading it throws a runtime error (NG0950).
Reading too early
This component fails as soon as it is created, because the constructor reads a required input:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-broken',
template: `<p>{{ name() }}</p>`,
})
export class Broken {
name = input.required<string>();
constructor() {
// Throws NG0950: the input has no value yet.
console.log(this.name());
}
}The same applies to a field initializer that calls this.name() directly. A plain initializer runs once, at construction, and never again.
Where reading is safe
You can read an input safely in these places:
- The template. Angular renders it after the inputs are set.
ngOnInit()and later lifecycle hooks.- Inside
computed(). The function runs lazily, on the first read, and again when its dependencies change. - Inside
effect(). Effects created in a constructor run later, during change detection, after the inputs are set. - Event handlers, because they run after the component exists.
The following component uses each of the safe options:
import { Component, computed, effect, input, OnInit } from '@angular/core';
@Component({
selector: 'app-greeting',
template: `<p>{{ message() }}</p>`,
})
export class Greeting implements OnInit {
name = input.required<string>();
message = computed(() => `Hello, ${this.name()}!`);
constructor() {
effect(() => {
console.log('name is now', this.name());
});
}
ngOnInit() {
console.log('first value:', this.name());
}
}If the parent binds [name]="user.name", the effect logs the first value once, then again each time user.name changes. ngOnInit runs once only.
Reacting to later changes
ngOnInit gives you the first value only. To follow every change, put the logic in a computed() when you are deriving a value. Put it in an effect() when you need a side effect, such as logging or calling a service. Both track the signals they read, so you do not compare old and new values yourself.
ngOnChanges still works with signal inputs, and SimpleChanges lists them by property name. New code rarely needs it, because signals track dependencies for you.
Inputs are read-only
A signal input has no set() or update() method. A child cannot assign to it, and each new value must come from the parent's binding. If the child needs to change the value, use model(), which is covered in an earlier chapter, or keep a separate local signal. Initialize that signal inside ngOnInit or an effect, not in the field initializer.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| NG0950 when the component is created | A required input was read in the constructor or a field initializer | Read it in `ngOnInit`, `computed()`, `effect()`, or the template |
| A derived field never updates | The value was copied once from the input | Use `computed()` |
| The default value appears briefly | An optional input was read before the parent bound a value | Treat the default as the initial state, or make the input required |
Exercise
A component declares id = input.required<number>() and label = Item ${this.id()}`` as a plain field. What happens when the component is created, and how do you fix it?
Show answer
Creating the component throws NG0950. The field initializer runs during construction, before Angular has set the input. Even if it worked, the string would be computed once and never update. Declare label = computed(() => Item ${this.id()}) instead, so the read happens lazily and re-runs whenever id changes.