AI Hints
Home›Angular Tutorial›output(), Sending Data Up and model() for Two-Way Binding
Angular · Lesson 51 of 120

model() for Two-Way Binding

Both ways at once.

⏱ 2 min read▶ Video at 1:00:12🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Some components edit a value that the parent owns. A quantity picker, or a rating. The value must flow down, and changes must flow back up. Model does both.

  • Edit the parent's value
  • Down and up
  • model

model()

Model is an input, that the child can also change. The parent binds it with banana in a box, square brackets around round brackets. When the child changes it, the parent's signal changes too. And when the parent changes it, the child sees it.

keyvalue
value = model(1)a two-way input
[(value)]="copies"banana in a box
Child changesparent's signal updates
Parent changeschild updates

A quantity picker

Quantity has a model called value. The parent binds its copies signal to it, with banana in a box. We click plus three times. Then the parent resets to one, and we click plus once more. Run it. The parent saw every change from the child. And the child followed the parent's reset.

app.tstypescript
import { Component, signal } from '@angular/core';
import { Quantity } from './quantity';

@Component({
  selector: 'app-root',
  imports: [Quantity],
  template: `
    <h2>Emma</h2>
    <app-quantity [(value)]="copies" />
    <p>The parent sees: {{ copies() }} copies, £{{ (copies() * 6.99).toFixed(2) }}</p>
    <button id="reset" (click)="copies.set(1)">Parent resets to 1</button>
  `,
})
export class App {
  copies = signal(1);
}
Also uses quantity.ts
import { Component, model } from '@angular/core';

@Component({
  selector: 'app-quantity',
  template: `
    <button id="minus" (click)="value.update(v => v - 1)">−</button>
    <b> {{ value() }} </b>
    <button id="plus" (click)="value.update(v => v + 1)">+</button>
  `,
})
export class Quantity {
  value = model(1);
}
Also uses actions.json
[{"click": "#plus", "times": 3}, {"click": "#reset"}, {"click": "#plus"}]

Summary

Model creates a two-way input, and banana in a box binds it to a parent signal. Next, content projection.

Key takeaways
  • model(1)
  • [(value)]
  • Both directions
Next up: Content Projection — ng-content