← Back to Angular, page 6

angular-0105 · Easy fresher

How does two-way binding work in Angular, and how do [(ngModel)] and model() relate to property and event binding?

Two-way binding is a shortcut for a property binding plus an event binding. [(x)]="value" means [x]="value" (xChange)="value = $event". ngModel uses this for form controls, and a component can offer it with a model() signal or an input plus an output named xChange.

Full answer

Two-way binding keeps a parent value and a child control in sync. The syntax [(x)]="value" is nicknamed "banana in a box". It is only sugar for two bindings:

[x]="value" (xChange)="value = $event"

The child must therefore have an input x and an output named xChange. Angular does not do anything magical. The parent value flows down through the input, and when the child emits xChange, the parent assigns the new value.

ngModel (from FormsModule) is the best known case. [(ngModel)]="name" sets the control value from name, and updates name on every input event. It works with a ControlValueAccessor behind the scenes, so it works with inputs, selects, checkboxes and custom controls.

Custom two-way binding in a component:

  • Modern way: value = model(0) creates a writable signal that is also an input and an output (valueChange). Use [(value)]="parentSignal", where the parent can pass a signal directly. Inside, this.value.set(5) updates the parent.
  • Classic way: an @Input() value plus an @Output() valueChange = new EventEmitter().

Notes:

  • With a signal in the parent, bind with [(value)]="score" where score is a WritableSignal. Angular reads and writes it for you in a two-way binding.
  • You cannot use two-way binding on an expression that cannot be assigned, for example a function call or a pipe.
  • For forms with validation, prefer reactive forms. ngModel is simple but gives less control, and mixing ngModel with formControl on the same element is not allowed.
  • Two-way binding hides data flow. In large apps, prefer one-way down and events up, using two-way only for small form-like components.
typescript
import { Component, model, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-stepper',
  template: `
    <button (click)="value.set(value() - 1)">-</button>
    {{ value() }}
    <button (click)="value.set(value() + 1)">+</button>
  `,
})
export class StepperComponent {
  readonly value = model(0); // input "value" + output "valueChange"
}

@Component({
  selector: 'app-two-way-demo',
  imports: [StepperComponent, FormsModule],
  template: `
    <app-stepper [(value)]="marks" />
    <p>Marks: {{ marks() }}</p>

    <input [(ngModel)]="name" name="first" />
    <!-- long form of the line above: -->
    <input [ngModel]="name" (ngModelChange)="name = $event" name="second" />
    <p>{{ name }}</p>
  `,
})
export class TwoWayDemoComponent {
  readonly marks = signal(10);
  name = 'Asha';
}

Angular 20.3, strict template check passes. A Karma test clicks the child button and checks that the parent signal and the parent text change.

Key points

  • `[(x)]` equals `[x]` plus `(xChange)`.
  • `ngModel` is the standard form directive using this pattern. It needs `FormsModule`.
  • `model()` creates a signal that is both input and output.
  • Prefer one-way data flow for large state.

Common mistakes

  • Forgetting to import `FormsModule` and getting an error about `ngModel`.
  • Naming the output incorrectly, so `[(x)]` does not work.
  • Using two-way binding on an expression that cannot be assigned.

Follow-ups

  • How would you write the long form of `[(ngModel)]`?
  • How does `model()` differ from `input()` plus `output()`?
  • Why mix `ngModel` and reactive forms carefully?

Tips

  • Show the expanded form. It proves you understand it.
  • Mention `model()` as the modern option.
  • Add a sentence on one-way flow as the default.

Evaluation rubric

  • Explains the banana syntax as two bindings.
  • Knows ngModel, model(), and the classic pair.
  • Mentions trade-offs.