AI Hints
Home›Angular Tutorial›Components, Templates and Interpolation
Angular · Lesson 20 of 120

Templates

HTML with super powers.

⏱ 2 min read▶ Video at 24:19🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

A template is mostly normal HTML. But Angular adds a few special symbols, so the HTML can show data, and react to the user.

  • Mostly normal HTML
  • A few special symbols
  • Data and events

Template syntax

Double curly braces show a value. Square brackets bind a property. Round brackets listen for an event. And at if, and at for, decide what to show. We'll learn each of these, one at a time.

SyntaxDoesExample
{{ }}show a value{{ title }}
[ ]set a property[disabled]="soldOut"
( )listen to an event(click)="buy()"
@if / @forcontrol what shows@for (b of books)

app.html

This template lives in its own file, app dot html. It uses double curly braces twice, to show the shop name, and the number of books. Everything else is plain HTML.

app.htmlhtml
<h1>{{ shopName }}</h1>
<p>We have {{ booksInStock }} books in stock.</p>
<button>Browse books</button>

app.ts

Template url points to the file. The two properties, shop name, and books in stock, belong to the class. The template can read them by name. Run it. The values from the class appear in the page.

app.tstypescript
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.html',
})
export class App {
  shopName = 'The Bookshop';
  booksInStock = 1250;
}
Also uses app.html
<h1>{{ shopName }}</h1>
<p>We have {{ booksInStock }} books in stock.</p>
<button>Browse books</button>

Summary

Templates are HTML, plus curly braces, square brackets, round brackets, and control flow. They can read the class's properties. Next, interpolation.

Key takeaways
  • templateUrl
  • {{ shopName }}
  • Class properties
Next up: Interpolation — showing values