In Angular, templates are a crucial part of components. They define the view for the component and determine how the component is rendered in the browser. This section will cover the basics of component templates, including how to create and use them effectively.
Key Concepts
- Template Syntax: Understanding the syntax used in Angular templates.
- Data Binding: Binding data from the component class to the template.
- Event Binding: Handling user events in the template.
- Template Expressions: Using expressions within the template.
- Template Reference Variables: Accessing DOM elements and component instances.
Template Syntax
Angular templates use a combination of HTML and Angular-specific syntax to define the view. Here is a basic example of a component template:
<!-- app.component.html --> <h1>{{ title }}</h1> <p>Welcome to {{ title }}!</p> <button (click)="changeTitle()">Change Title</button>
Explanation
- Interpolation (
{{ }}
): Used to bind data from the component class to the template. - Event Binding (
(event)="handler()"
): Used to bind events like clicks to methods in the component class.
Data Binding
Data binding in Angular allows you to synchronize data between the component class and the template. There are four types of data binding:
- Interpolation: Binding data from the component to the template.
- Property Binding: Binding properties of DOM elements to component properties.
- Event Binding: Binding events to methods in the component.
- Two-way Binding: Combining property and event binding to create a two-way data flow.
Example
// app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Angular App'; changeTitle() { this.title = 'New Title'; } }
<!-- app.component.html --> <h1>{{ title }}</h1> <input [value]="title" (input)="title = $event.target.value"> <button (click)="changeTitle()">Change Title</button>
Explanation
- Interpolation (
{{ title }}
): Displays thetitle
property from the component. - Property Binding (
[value]="title"
): Binds thevalue
property of the input element to thetitle
property. - Event Binding (
(input)="title = $event.target.value"
): Updates thetitle
property when the input value changes.
Template Expressions
Template expressions are used within interpolation and binding syntax to perform calculations or access properties and methods.
Example
<!-- app.component.html --> <p>The length of the title is: {{ title.length }}</p> <p>{{ getTitleMessage() }}</p>
Explanation
- Expression (
{{ title.length }}
): Calculates the length of thetitle
string. - Method Call (
{{ getTitleMessage() }}
): Calls a method from the component class.
Template Reference Variables
Template reference variables allow you to reference DOM elements and component instances within the template.
Example
<!-- app.component.html --> <input #titleInput type="text"> <button (click)="logTitle(titleInput.value)">Log Title</button>
Explanation
- Template Reference Variable (
#titleInput
): References the input element. - Using the Variable (
titleInput.value
): Accesses the value of the input element.
Practical Exercise
Task
Create a component that displays a list of items and allows the user to add new items to the list.
Steps
- Create a new component called
ItemListComponent
. - Define a template that displays the list of items.
- Add an input field and a button to add new items to the list.
- Implement the necessary data binding and event handling.
Solution
// item-list.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-item-list', templateUrl: './item-list.component.html', styleUrls: ['./item-list.component.css'] }) export class ItemListComponent { items: string[] = ['Item 1', 'Item 2', 'Item 3']; newItem: string = ''; addItem() { if (this.newItem) { this.items.push(this.newItem); this.newItem = ''; } } }
<!-- item-list.component.html --> <h2>Item List</h2> <ul> <li *ngFor="let item of items">{{ item }}</li> </ul> <input [(ngModel)]="newItem" placeholder="New Item"> <button (click)="addItem()">Add Item</button>
Explanation
*ngFor
Directive: Iterates over theitems
array and displays each item.- Two-way Binding (
[(ngModel)]="newItem"
): Binds the input field to thenewItem
property. - Event Binding (
(click)="addItem()"
): Calls theaddItem
method when the button is clicked.
Summary
In this section, you learned about Angular component templates, including template syntax, data binding, event binding, template expressions, and template reference variables. You also completed a practical exercise to reinforce these concepts. Understanding and effectively using component templates is essential for building dynamic and interactive Angular applications.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces