Data Binding: Interpolation, Property, Event and Two-Way
Four Directions of Data
| Type | Syntax | Direction |
|---|---|---|
| Interpolation | {{ value }} | Component to template (text) |
| Property binding | [property]="value" | Component to template (element property) |
| Event binding | (event)="handler()" | Template to component |
| Two-way binding | [(ngModel)]="value" | Both ways |
1. Interpolation
typescript
export class Profile {
name = 'Ana';
price = 49.5;
tags = ['new', 'sale'];
total() { return this.price * 2; }
}
html
<h1>Hello, {{ name }}!</h1>
<p>Price x2 = {{ total() }}</p>
<p>First tag: {{ tags[0] }}</p>
<p>Upper: {{ name.toUpperCase() }}</p>
<p>2 + 2 = {{ 2 + 2 }}</p>
Only expressions: Inside
{{ }} you can use expressions, not statements. No if, no =, no new. Keep them short and move logic into the class.
2. Property Binding
Square brackets set a DOM property from your data.
html
<img [src]="imageUrl" [alt]="title">
<button [disabled]="isSaving">Save</button>
<input [value]="name">
<a [href]="profileLink">Profile</a>
<div [hidden]="!visible">Visible?</div>
Interpolation vs property:
disabled="{{ isSaving }}" does not work as you expect because an attribute with any text disables the button. Use [disabled]="isSaving".
3. Class, Style and Attribute Binding
html
<div [class.active]="isActive">Single class</div>
<div [class]="{ active: isActive, error: hasError }">Several classes</div>
<p [style.color]="isError ? 'red' : 'green'">Colored text</p>
<div [style.width.px]="barWidth">Bar</div>
<td [attr.colspan]="columns">Cell</td>
<button [attr.aria-label]="label">X</button>
4. Event Binding
typescript
export class Counter {
count = 0;
inc() { this.count++; }
onKey(event: KeyboardEvent) { console.log(event.key); }
onInput(event: Event) {
this.name = (event.target as HTMLInputElement).value;
}
name = '';
}
html
<button (click)="inc()">Clicked {{ count }} times</button>
<input (keyup.enter)="save()" (input)="onInput($event)">
<form (submit)="submit($event)">...</form>
<div (mouseenter)="hover = true" (mouseleave)="hover = false">Hover me</div>
$event is the browser event object. Key filters like keyup.enter and keydown.escape save code.
5. Two-Way Binding with ngModel
typescript
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-hello',
imports: [FormsModule],
template: `
<input [(ngModel)]="name" placeholder="Your name">
<p>Hello, {{ name || 'stranger' }}!</p>
`,
})
export class Hello { name = ''; }
[(ngModel)] is the "banana in a box" syntax: it combines [ngModel] and (ngModelChange). It needs FormsModule.
Template Reference Variables
html
<input #email placeholder="Email">
<button (click)="subscribe(email.value)">Subscribe</button>
Safe Navigation
html
<p>{{ user?.address?.city }}</p>
<p>{{ user?.nickname ?? 'No nickname' }}</p>
Avoid heavy functions in templates: A function in
{{ }} may run on every change check. Use signals with computed() (next lessons) or pure pipes for expensive calculations.
Conclusion
Use {{ }} for text, [prop] to set properties, (event) to react to the user and [(ngModel)] for simple two-way input. Remember FormsModule for ngModel.
Quick Summary: {{ }} text, [x] property, (x) event, [(x)] both ways.

