Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Angular Templates


Templates are the HTML that a component renders.

Templates combine plain HTML with Angular template syntax to show data and react to user events.


Components, Templates, and Selectors

  • A component is a class that controls a view (its template).
  • Each component has a selector (e.g., app-root) that you place in HTML.
  • The root component renders inside index.html's <app-root>.

How templates work

  1. Initial render: Angular creates the component and processes the template, wiring bindings between the DOM and component fields.
  2. Change detection: On user events, timers, or async work, Angular re-evaluates template expressions and updates only what changed.
  3. Directives: Create/destroy embedded views (DOM fragments) based on state.
  4. Binding flow: Interpolation/Property binding push data to the view. Event binding captures browser events back to the component.

Note: Interpolation escapes HTML.

Use property/attribute bindings for dynamic attributes. Use ?. to avoid null errors.

Syntax

import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h3>Hello {{ name }}</h3>
    <button (click)="name = 'Angular'">Reset</button>
  `
})
export class App {
  name = 'World';
}

bootstrapApplication(App);
<app-root></app-root>

Run Example »

Example explained

  • {{ name }}: Interpolation reads the name field and inserts it as text.
  • (click): Event binding updates the component state when the button is clicked.

Note: Template expressions run in the component context.

Keep them fast and side-effect free; do work in the class, not inline in the template.


Template Essentials

Key concepts:

  • Templates are the HTML that Angular renders for a component.
  • Bindings: interpolation {{ ... }}, property [prop], and event (event).
  • Template refs: local names like #box to reference elements or directives.
  • Structural directives (*ngIf, *ngFor) change the DOM layout.
<p>Hello {{ name }}</p>
<img [src]="url" (error)="onMissing()">
<input #box (input)="save(box.value)">
<li *ngFor="let item of items; index as i">{{ i }}. {{ item }}</li>

Code explained

  • {{ name }}: Interpolation for text.
  • [src]: Property binding to set an element property.
  • (error): Event binding to react to the image error event.
  • #box: Template reference variable; box.value reads the current input value.
  • *ngFor ... index as i: Structural directive to loop with the zero-based index.

Note: See Data Binding for property/class/style binding, and Conditional Rendering for showing/hiding content.


REMOVE ADS


Templates Overview

Explore templates step by step in these next chapters:



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.