Template Syntax
| Since: | Angular 17(2023) |
|---|
Angular's template syntax is a mechanism that adds Angular-specific notation to regular HTML, enabling you to bind component class data to the view and declaratively describe showing/hiding DOM elements and list repetition.
Syntax Overview
| Syntax | Notation | Description |
|---|---|---|
| Interpolation binding | {{ expression }} | Displays the value of a component property or expression as text. |
| Property binding | [property]="expression" | Dynamically sets a value on an HTML element or component property. |
| Event binding | (eventName)="handler" | Calls a component method when a DOM event occurs. |
| Two-way binding | [(ngModel)]="property" | Synchronizes form input and a component property bidirectionally. |
| Conditional display | *ngIf="condition" | Adds the element to the DOM only when the condition is true. |
| Repetition | *ngFor="let item of list" | Loops through array elements in order to generate elements. |
| Class binding | [class.className]="condition" | Applies the specified CSS class only when the condition is true. |
| Style binding | [style.property]="value" | Dynamically sets an inline style value. |
| Template reference variable | #variableName | References a DOM element or component within the template as a variable. |
Interpolation, Property Binding, and Event Binding
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>{{ title }}</h1>
<!-- Dynamically sets the imageUrl property value to the src attribute -->
<img [src]="imageUrl" [alt]="imageAlt">
<!-- Clicking the button calls the changeTitle() method -->
<button (click)="changeTitle()">Change title</button>
`,
})
export class AppComponent {
title: string = 'Initial title';
imageUrl: string = 'https://example.com/logo.png';
imageAlt: string = 'Logo image';
changeTitle(): void {
this.title = 'Updated title';
}
}
Two-way Binding (ngModel)
// app.component.ts
// Two-way binding requires importing FormsModule
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
standalone: true,
imports: [FormsModule],
selector: 'app-root',
template: `
<input [(ngModel)]="name" placeholder="Enter your name">
<p>Hello, {{ name }}!</p>
`,
})
export class AppComponent {
name: string = '';
}
Conditional Display (*ngIf) and Repetition (*ngFor)
// app.component.ts
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
standalone: true,
imports: [CommonModule],
selector: 'app-root',
template: `
<div *ngIf="isLoggedIn; else loginBlock">
<p>You are logged in.</p>
</div>
<!-- This template is shown when isLoggedIn is false -->
<ng-template #loginBlock>
<p>Please log in.</p>
</ng-template>
<ul>
<!-- Receives each element of the fruits array into the fruit variable, also gets the index -->
<li *ngFor="let fruit of fruits; let i = index">
{{ i + 1 }}. {{ fruit }}
</li>
</ul>
`,
})
export class AppComponent {
isLoggedIn: boolean = false;
fruits: string[] = ['apple', 'banana', 'orange'];
}
Class Binding and Style Binding
// app.component.ts
import { Component } from '@angular/core';
@Component({
standalone: true,
selector: 'app-root',
template: `
<p [class.active]="isActive" [class.highlight]="isHighlight">
Text with dynamically switching classes.
</p>
<p [style.color]="textColor" [style.font-size.px]="fontSize">
Text with dynamically changing styles.
</p>
<button (click)="toggleActive()">Toggle active</button>
`,
styles: [`
.active { font-weight: bold; }
.highlight { background-color: yellow; }
`],
})
export class AppComponent {
isActive: boolean = false;
isHighlight: boolean = true;
textColor: string = 'royalblue';
fontSize: number = 18;
toggleActive(): void {
this.isActive = !this.isActive;
}
}
Template Reference Variables
// app.component.ts
import { Component } from '@angular/core';
@Component({
standalone: true,
selector: 'app-root',
template: `
<!-- #inputRef allows referencing this input element's DOM object within the template -->
<input #inputRef type="text" placeholder="Enter text">
<!-- On button click, the value can be obtained directly via inputRef.value -->
<button (click)="showValue(inputRef.value)">Show value</button>
<p>Input value: {{ displayValue }}</p>
`,
})
export class AppComponent {
displayValue: string = '';
showValue(value: string): void {
this.displayValue = value;
}
}
Overview
Angular's template syntax allows you to declaratively connect HTML with the properties and methods of the component class. Combining {{ }} for interpolation, [] for property binding, () for event binding, and [()] for two-way binding lets you express dynamic UIs simply.
*ngIf and *ngFor are called structural directives and manipulate the DOM structure itself. Because conditional branching and repetition can be described as HTML attributes, the screen structure becomes easy to grasp just by reading the template.
Since Angular 17, new control flow syntax (@if / @for) has been introduced as an alternative to *ngIf / *ngFor, enabling more intuitive descriptions.
For passing data between components, see @Input / @Output. For more detailed form control, see Reactive Forms.
If you find any errors or copyright issues, please contact us.