Sub topic 1
How Angular Templates Connect to Component State
An Angular component has two parts. The TypeScript class holds state and behavior. The template is the HTML that describes what the user sees. Data binding is the set of template syntax forms that connect these two parts, so a change in the class appears on screen and user actions reach the class.
This chapter gives you the working model the rest of the tutorial builds on: where template expressions run, which way data flows, and which binding form fits which job.
Prerequisites
This chapter assumes you already know:
- HTML and DOM basics, including the difference between an element and its properties
- TypeScript classes, fields, methods and type annotations
- How to create and run an Angular component with the Angular CLI
- How JavaScript events such as
clickandinputwork
The Component Context
Every expression in a template runs against the component instance. When the template says user.name, Angular reads this.user.name from the class. You never write this in a template, and you can't reach globals such as window or document from it.
import { Component } from '@angular/core';
@Component({
selector: 'app-status',
template: `
<p>{{ user.name }} has {{ tasks.length }} tasks</p>
<button [disabled]="tasks.length === 0" (click)="clear()">Clear</button>
`,
})
export class StatusComponent {
user = { name: 'Ada' };
tasks = ['write', 'review'];
clear() {
this.tasks = [];
}
}This one template uses three binding forms. {{ }} renders text. [disabled] sets a DOM property. (click) runs a method. When clear() empties the array, Angular re-evaluates the expressions: the text changes to "0 tasks" and the button becomes disabled.
Directions of Data Flow
Each binding form moves data in a fixed direction.
| Syntax | Name | Direction |
|---|---|---|
| `{{ expr }}` | Interpolation | Class to view |
| `[prop]="expr"` | Property binding | Class to view |
| `(event)="statement"` | Event binding | View to class |
| `[(prop)]="field"` | Two-way binding | Both ways |
Two-way binding is a shorthand for a property binding plus an event binding. It doesn't use a separate mechanism, and you will take it apart in a later chapter.
Expressions Versus Statements
Angular parses the right-hand side of a binding in one of two modes.
- Template expressions appear in
{{ }}and[prop]. They produce a value. They can't contain assignments (=,+=),++,newor;. - Template statements appear in
(event). They perform an action, so assignments and method calls are allowed, for example(click)="count = count + 1".
Angular evaluates expressions every time it checks the component for changes, which can happen many times per second. Keep expressions free of side effects and cheap to compute.
What Comes Next
The following chapters cover each binding form in detail. You'll start with interpolation and property binding, then move to events, two-way binding, communication between parent and child components, and signals.
Exercise
In the template <button [disabled]="count > 3" (click)="count = count + 1">Add</button>, which part is a template expression, which is a template statement, and why would [disabled]="count = 5" fail?
Show answer
count > 3 is a template expression because it sits in a property binding and produces a value. count = count + 1 is a template statement because it sits in an event binding and performs an action. [disabled]="count = 5" fails because template expressions can't contain assignments, so the template parser rejects it.
Exercise
A template shows {{ computeReport() }}, and computeReport() loops over 10,000 records. Why is this a problem?
Show answer
Angular re-evaluates template expressions on every change detection check, so the loop runs again each time. Compute the value once when the data changes and store it in a field (or a computed signal), then bind to that.