Welcome to the Angular Template Statements tutorial! In this lesson, we'll dive into one of the key features of Angular - Data Binding. We'll explore how to use Angular's template statements to display and manipulate data in our application.
By the end of this tutorial, you'll be able to:
Before we dive into template statements, let's ensure you have Angular set up. Follow the official Angular guide to install Angular CLI on your machine.
After installation, create a new project:
ng new template-statementsChange into the project directory:
cd template-statementsNow, let's explore the different types of Angular Template Statements.
Interpolation is the simplest way to display data in an Angular template. It allows us to embed expressions directly into our HTML using double curly braces ({{ }}).
In your app component, add the following code:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>Welcome to {{ title }}!</h1>
`
})
export class AppComponent {
title = 'CodeYourCraft';
}This code sets the title of our application to 'CodeYourCraft' and displays it in an h1 tag using interpolation.
Run the application:
ng serveVisit http://localhost:4200 in your browser to see the result.
Property binding is used to bind properties of an HTML element with properties of a component. It allows us to dynamically set properties of HTML elements in our template.
Let's create a component with a custom property and use property binding to set the class attribute of an HTML element:
import { Component } from '@angular/core';
@Component({
selector: 'app-highlight',
template: `
<h4 [class.highlight]="isHighlighted">{{ text }}</h4>
`
})
export class HighlightComponent {
text = 'Hello World';
isHighlighted = true;
}Now, use the HighlightComponent in your app component:
<app-highlight [text]="title"></app-highlight>In this example, the title property of the app component is passed to the text property of the HighlightComponent using property binding. The component also sets the class of the h4 element based on the isHighlighted property using property binding.
Event binding allows us to handle events in our components. It lets us respond to user interactions like clicks, hover events, and more.
Let's create a component that displays a button and logs a message when clicked:
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'app-button',
template: `
<button (click)="onClick()">Click me!</button>
`
})
export class ButtonComponent {
@Output() clickEvent = new EventEmitter();
onClick() {
this.clickEvent.emit('Button clicked!');
}
}Now, use the ButtonComponent in your app component:
<app-button (click)="onButtonClicked($event)"></app-button>In this example, the ButtonComponent emits a click event when the button is clicked. The onButtonClicked function in the app component catches the event and logs the message.
Two-way data binding allows data to flow both ways between the component and the template. In other words, changes in the template update the component's properties, and changes in the component's properties update the template.
Let's create a component that displays an input field and updates the component's property when the input changes:
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-input',
template: `
<input [(ngModel)]="inputValue" type="text" [(ngModelOptions)]="{standalone: true}">
`
})
export class InputComponent {
@Input() placeholder: string;
@Output() inputChange = new EventEmitter();
inputValue: string;
ngOnChanges(changes: any) {
if (changes.placeholder) {
this.inputValue = changes.placeholder.currentValue;
}
}
onInputChange() {
this.inputChange.emit(this.inputValue);
}
}Now, use the InputComponent in your app component:
<app-input placeholder="Enter your name"></app-input>In this example, the InputComponent uses two-way data binding to update the inputValue property when the input changes. The component also emits an inputChange event when the input changes.
What does Interpolation allow us to do in an Angular template?
What is Two-Way Data Binding in Angular?