A template is the HTML that describes what a component renders, extended with Angular-specific syntax for binding data and adding logic. This lesson covers the core template syntax you will use in every Angular view.
What Is an Angular Template?
A template is ordinary HTML augmented with Angular syntax: interpolation for text, bindings for properties and events, and directives for structure and behavior. Angular compiles templates into efficient render instructions rather than interpreting them at runtime.
Templates can be written inline as a string on the @Component decorator, or, more commonly for anything beyond a couple of lines, in a separate .html file referenced via templateUrl.
Interpolation renders a string result of an expression between tags or in an attribute.
Property bindings use square brackets and update a DOM property whenever the expression changes.
Event bindings use parentheses and run a statement when the event fires.
Structural directives like *ngFor and *ngIf add or remove elements from the DOM.
Angular Template Syntax Cheatsheet
The syntax forms you will use constantly while writing templates.
Syntax
Example
Purpose
Interpolation
{{ user.name }}
Render a text value
Property binding
[src]="imageUrl"
Bind a DOM property
Event binding
(click)="save()"
Handle a DOM event
Two-way binding
[(ngModel)]="name"
Bind and update a value together
*ngIf
*ngIf="loggedIn"
Conditionally render an element
*ngFor
*ngFor="let x of items"
Repeat an element for each item
@if (new)
@if (loggedIn) { ... }
Modern conditional control flow
@for (new)
@for (x of items; track x.id) { ... }
Modern loop control flow
Template ref var
#nameInput
Reference a DOM element/component in the template
Interpolation and Template Expressions
Interpolation evaluates a JavaScript-like expression and inserts the string result into the DOM. Expressions can access any public property or method on the component class, but should stay simple, templates are not the place for complex logic.
Classic Angular uses structural directives prefixed with *, like *ngIf and *ngFor. Since Angular 17, a new built-in control flow syntax (@if, @for, @switch) offers the same capability with cleaner syntax and better performance, without needing to import CommonModule.
<!-- classic -->
<p *ngIf="user">{{ user.name }}</p>
<li *ngFor="let item of items">{{ item.name }}</li>
<!-- modern control flow -->
@if (user) {
<p>{{ user.name }}</p>
}
@for (item of items; track item.id) {
<li>{{ item.name }}</li>
}
@for requires a track expression, usually a unique id, to efficiently reconcile list changes.
Template Reference Variables
A template reference variable, declared with #name, gives you direct access to a DOM element or a component/directive instance from within the same template, useful for reading input values without two-way binding.
Writing complex, multi-step logic directly inside template expressions instead of a component method.
Forgetting track in the new @for syntax, which is required (unlike the optional trackBy in *ngFor).
Mixing *ngIf/*ngFor and @if/@for inconsistently across a codebase without a clear migration plan.
Using template reference variables across separate templates, they are only valid within the template they are declared in (with narrow exceptions).
Key Takeaways
Templates are HTML extended with interpolation, bindings, and directives.
Property, event, and two-way bindings connect a template to component class properties and methods.
The modern @if/@for/@switch control flow is the recommended replacement for *ngIf/*ngFor/*ngSwitch.
Template reference variables (#name) expose elements or directives to other bindings in the same template.
Pro Tip
Prefer the new @if/@for control flow syntax in new code, it is faster at runtime, requires no imports, and produces clearer compiler error messages than the older structural directives.
You now understand core template syntax. Next, dig deeper into Data Binding to see how values flow between your class and your view.