Video lecture
Watch on YouTube(opens in a new tab)Sub topic 9
Templates, Interpolation and Binding
This chapter assumes you have already built a standalone component and know where its template lives. It also assumes you are comfortable with HTML, with the difference between DOM properties and HTML attributes, and with basic TypeScript classes.
A template is the HTML that a component renders. Angular extends plain HTML with a small syntax that links the template to the component class. Data moves from the class into the DOM, and events move from the DOM back into the class.
The Working Model
Each binding form has its own syntax:
| Syntax | Direction | Example |
|---|---|---|
| `{{ expr }}` | Class to text | `{{ name }}` |
| `[prop]="expr"` | Class to DOM property | `[disabled]="isSaving"` |
| `[attr.x]="expr"` | Class to HTML attribute | `[attr.aria-label]="label"` |
| `[class.x]="expr"` | Toggle a CSS class | `[class.active]="isActive"` |
| `[style.x]="expr"` | Set one style | `[style.color]="color"` |
| `(event)="stmt"` | DOM to class | `(click)="save()"` |
A Worked Example
import { Component } from '@angular/core';
@Component({
selector: 'app-profile-card',
template: `
<h2>{{ name }}</h2>
<p>Member for {{ currentYear - joinedYear }} years</p>
<img [src]="avatarUrl" [alt]="name + ' avatar'" width="64" />
<input [value]="name" (input)="onNameInput($event)" />
<p [class.active]="isActive" [style.color]="isActive ? 'green' : 'gray'">
{{ isActive ? 'Active' : 'Inactive' }}
</p>
<button
[disabled]="isSaving"
[attr.aria-label]="'Save profile for ' + name"
(click)="save()">
{{ isSaving ? 'Saving…' : 'Save' }}
</button>
`,
styles: `.active { font-weight: bold; }`,
})
export class ProfileCard {
name = 'Ada';
joinedYear = 2019;
currentYear = new Date().getFullYear();
avatarUrl = 'https://picsum.photos/64';
isActive = true;
isSaving = false;
onNameInput(event: Event) {
this.name = (event.target as HTMLInputElement).value;
}
save() {
this.isSaving = true;
setTimeout(() => (this.isSaving = false), 1000);
}
}When you type in the input, the input event calls onNameInput, which updates name. Angular then re-renders every binding that reads name, including the heading and the button's label. $event is the native DOM event object.
The fields here are plain class properties. The signals chapter later replaces them with reactive state.
Common Errors
Forgetting the brackets
<button disabled="isSaving"> sets the attribute to the literal string "isSaving", and a non-empty string makes the button disabled forever. Without brackets the value is a string. With brackets the value is a TypeScript expression.
Property versus attribute
Property binding targets DOM properties, not HTML attributes. Some attributes, such as colspan and all aria-* attributes, have no matching property in the form Angular expects. Writing [aria-label] produces a template error. Use [attr.aria-label] instead.
Heavy logic in expressions
Angular can re-evaluate template expressions on every change detection pass. Keep them short and free of side effects. Avoid calls like {{ computeReport() }} that do expensive work. Template expressions also cannot use assignments (=), new, or global objects such as window.
Untyped `$event`
$event.target.value fails type checking under strict mode because target is typed as EventTarget | null. Cast it inside the class method, as onNameInput does above.
Exercise
A developer writes <img src="{{ avatarUrl }}"> in one place and <img [src]="avatarUrl"> in another. Do they behave the same, and when would interpolation in an attribute not work?
Show answer
For string values such as src they behave the same, because Angular turns the interpolation into a property binding. Interpolation always produces a string, though, so it fails for non-string properties: [disabled]="false" passes a boolean, while disabled="{{ false }}" passes the string "false". For attributes with no matching DOM property, such as aria-label, use [attr.aria-label].
Exercise
Add a "Reset" button to ProfileCard that sets name back to "Ada" and is disabled when name is already "Ada".
Show answer
Add <button [disabled]="name === 'Ada'" (click)="reset()">Reset</button> to the template and reset() { this.name = 'Ada'; } to the class.