Input signals let a component receive data from outside. Model signals help track and update data inside the component easily.
Input signals and model signals in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
import { Component, Input, signal } from '@angular/core'; @Component({ selector: 'app-child', standalone: true, template: ` <p>Value: {{ value() }}</p> ` }) export class ChildComponent { @Input() value = signal(''); }
Input decorator marks a property as an input to receive data from a parent.
signal() creates a reactive value that updates the UI when changed.
name input as a signal with a default value.import { Component, Input, signal } from '@angular/core'; @Component({ selector: 'app-child', standalone: true, template: `<p>{{ name() }}</p>` }) export class ChildComponent { @Input() name = signal('Guest'); }
count is passed as input to show a reactive counter value.import { Component, Input, signal } from '@angular/core'; @Component({ selector: 'app-counter', standalone: true, template: ` <p>Count: {{ count() }}</p> ` }) export class CounterComponent { @Input() count = signal(0); }
This example shows a parent component passing a signal parentMessage to a child component's input message. Clicking the button changes the message reactively in the child.
import { Component, Input, signal } from '@angular/core'; @Component({ selector: 'app-child', standalone: true, template: ` <p>Message: {{ message() }}</p> ` }) export class ChildComponent { @Input() message = signal('Hello'); } @Component({ selector: 'app-root', standalone: true, imports: [ChildComponent], template: ` <app-child [message]="parentMessage"></app-child> <button (click)="changeMessage()">Change Message</button> ` }) export class AppComponent { parentMessage = signal('Welcome!'); changeMessage() { this.parentMessage.set('Hello from Parent!'); } }
Signals are functions you call to get the current value, like message().
Use .set(newValue) to update a signal's value and refresh the UI.
Input signals keep data reactive between parent and child components without extra code.
Input signals let components receive reactive data from parents.
Model signals inside components track and update data easily.
Using signals makes UI updates automatic and simple.
Practice
Input signals in Angular components?Solution
Step 1: Understand Input signals role
Input signals allow a component to get reactive data from its parent, keeping the data flow reactive and automatic.Step 2: Differentiate from other options
Sending events to parents is done by outputs, styling is unrelated, and user input events are handled differently.Final Answer:
To receive reactive data from parent components -> Option AQuick Check:
Input signals = receive reactive data [OK]
- Confusing input signals with output events
- Thinking input signals handle styling
- Assuming input signals manage user events
Solution
Step 1: Recall Angular input signal syntax
Input signals are declared with the@Input()decorator followed by a signal initialization.Step 2: Check each option
@Input() inputSignal = signal(); correctly uses@Input()decorator before the signal. const inputSignal = signal(); misses the decorator, C and D have invalid syntax.Final Answer:
@Input() inputSignal = signal(); -> Option AQuick Check:
Input signals need @Input() decorator [OK]
- Forgetting the @Input() decorator
- Placing @Input() incorrectly
- Using invalid syntax with signals
export class MyComponent {
@Input() count = signal(0);
increment() {
this.count.update(c => c + 1);
}
}What will be the value of
count() after calling increment() twice if the initial value is 0?Solution
Step 1: Understand initial signal value
The signalcountstarts at 0.Step 2: Apply increment method twice
Each call toincrement()updates the signal by adding 1, so after two calls: 0 + 1 + 1 = 2.Final Answer:
2 -> Option DQuick Check:
0 + 2 increments = 2 [OK]
- Thinking signals don't update automatically
- Confusing method calls with direct assignment
- Assuming initial value changes unexpectedly
export class SampleComponent {
@Input() data = signal();
ngOnInit() {
this.data.set('Hello');
}
}Solution
Step 1: Check signal initialization
The signaldatais declared without an initial value, which is invalid.Step 2: Understand signal requirements
Signals must have an initial value when created, sosignal()without arguments causes an error.Final Answer:
Missing initial value for signal() -> Option BQuick Check:
Signals need initial values [OK]
- Leaving signals uninitialized
- Thinking set() is disallowed on inputs
- Confusing lifecycle hooks usage
userName and also maintains a local model signal greeting that updates automatically when userName changes. Which approach correctly implements this behavior?Solution
Step 1: Understand reactive input and model signals
The input signaluserNameprovides reactive data from parent. The localgreetingshould react automatically to changes.Step 2: Use computed signal for automatic updates
Usingcomputedcreates a signal that updates wheneveruserName()changes, keepinggreetingin sync.Final Answer:
Use @Input() userName = signal(''); and greeting = computed(() => `Hello, ${this.userName()}`); -> Option CQuick Check:
Computed signals auto-update from input signals [OK]
- Manually updating signals instead of computed
- Using plain string input instead of signal
- Assigning greeting only once without reactivity
