Angular 13 Data Binding Example Tutorial
In this Angular tutorial, we'll go over the concept of data binding. You'll also learn how to tie data in an Angular web application to show on the UI using curly brackets.
Use Interpolation in Angular 13 Data Binding
SinceĀ AngularJS 1.0, data binding has been a feature of theĀ framework. Curly braces are used in the code to represent data binding -Ā {{variable goes here}}Ā ā and this process is known as interpolation.
A variable namedĀ {{ title }}Ā exists in theĀ app.component.htmlĀ file. The value of this variable is set in the app.component.ts file. We'll show the value inĀ app.component.htmlĀ later.
Our goal is to have the automobiles appear in a dropdown menu in the browser. We'll use the code segment listed below to accomplish this.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls:
})
export class AppComponent {
// Title variable declared
title = 'My Angular App';
// Defined cars array
cars = ;
}
To begin, we'll make a standard select tag with an option. We've chosen to use theĀ *ngForĀ loop as a tool. Using thisĀ *ngForĀ loop, we will iterate through the array of automobiles. As a result, an option tag will be produced with the values from the drop-down.
In Angular, we'll use the following syntax āĀ *ngFor = "let i for automobiles." The tag {{ i }} is used to retrieve the value of automobiles.
For data binding, we need two curly brackets, as previously explained. You declare the variables in theĀ app.component.tsĀ file. Curly brackets are used to retrieve the values afterwards.
Cars :
{{i}}
Now we'll open the browser and see the output of the above array of automobiles.
The variable is stored inĀ app.component.ts, and the value of the variable is retrieved using curly brackets inĀ app.component.htmlĀ - for example {{ }}.
When utilising Angular Data Binding, how do you use an If Statement?
It's past time for us to display info in the browser based on the condition. We've just created a variable and given it the value 'true.' We'll use the if statement to show/hide the data that will be displayed.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls:
})
export class AppComponent {
// isPresent var is declared
isPresent:boolean = true;
// Title variable declared
title = 'My Angular App';
// Defined cars array
cars = ;
}
Cars :
{{i}}
Statement is true.
Show me because isPresent is set to true.
If then Else using Angular 13 Data Binding
The variableĀ isPresentĀ will have the value 'false' assigned to it.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls:
})
export class AppComponent {
// isPresent var is declared
isPresent:boolean = false;
// Title variable declared
title = 'My Angular App';
// Defined cars array
cars = ;
}
We'll useĀ ng-templateĀ to display the else condition, as shown below.
Cars :
{{i}}
Statement is true.
NgTemplate condition is working!
Another If then Else Example
In theĀ app.component.tsĀ file, we will set theĀ isPresentĀ variable to true. The condition has been written as follows:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls:
})
export class AppComponent {
// isPresent var is declared
isPresent:boolean = true;
}
If the variable is true, condition 1 is true; otherwise, condition 2 is true. We have two templates with the ids #condition1 and #condition2 at this stage.
Statement is valid.
Statement is valid
Statement is invalid
I hope you will like the content and it will help you to learnĀ Angular 13 Data Binding Example Tutorial
If you like this content, do share.
Read the full article