How to use *ngIf else in AngularJS ?
Last Updated :
05 Jun, 2020
Introduction: The ngIf directive is used to show or hide parts of an angular application. It can be added to any tags, it is a normal HTML tag, template, or selectors. It is a structural directive meaning that it includes templates based on a condition constrained to boolean. When the expression evaluates to true it runs/displays the template given in the "then" clause. Or when the expression evaluates to false it displays the template given in the "else" clause. If there is nothing in else clause, it will by default display blank.
Syntax:
ngIf with an "else" block
html
<div *ngIf="condition; else elseStatement">
when condition is true.
</div>
<ng-template #elseStatement>
when condition is false.
</ng-template>
<!--It can be seen that the else
clause refers to ng-template,
with the label #elseStatement -->
It internally creates two
<ng-template>
, one for
"then" statement and another for
"else". So when the condition is true for ngIf, the content of the unlabelled
<ng-template>
gets displayed. And when false, the labelled
<ng-template>
runs.
Approach:
- As we know, ngIf else statement works on a variable that has a boolean type. Create an angular app and move to src/app. First, we need to define a variable say "check" in app.component.ts with a value true or false.
html
<!-- Define a variable say "check" with
value true/false in app.component.ts -->
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
check:boolean = true;
}
- After defining a variable, move to app.component.html and create two divisions using bootstrap classes. Move into the angular app and write command npm install bootstrap. The first division is for the "true" condition and the one inside <ng-template> is for the false condition. We have declared check to be true so we get a green division saying condition true. If the check was false, a red division saying condition false would be displayed.
html
<!-- *ngIf else -->
<div class="container-fluid">
<div class="row bg-success
text-light h1 justify-content-center
align-items-center" *ngIf="check;
else elseStatement"
style="height:150px">Condition true
</div>
<ng-template #elseStatement>
<div class="row bg-danger
text-light h1 d-flex
justify-content-center align-items-center"
style="height:150px">Condition false
</div>
</ng-template>
</div>
Output:
Output:
Advantages:
Similar Reads
How to Filter Multiple Values in AngularJS ? AngularJS is one of the popular frameworks of many web developers to create dynamic single-page web applications. To make the application more and more dynamic, we can use the filtering of data feature to dynamically show the data to the user as per the input or selection. These provide a better use
6 min read
How to apply filters to *ngFor in Angular ? In this article, we will see How to apply filters to *ngFor in AngularJS, along with understanding their basic implementation through the examples. NgFor is used as a Structural Directive that renders each element for the given collection each element can be displayed on the page. Implementing the f
3 min read
How to use filter within controllers in AngularJS ? In this article, we will see how to use the Filter inside the controller using AngularJS. Filters are used to format the value of an expression and display it to the user. It can be used in HTML Previews, Controllers or Services, and directives. AngularJS facilitates many built-in filters, although,
4 min read
How to Filter an Array based on user input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular filters can be added in AngularJS to format data.
3 min read
How to Use & Create custom directive in Angular? Directives in Angular are nothing but the classes that allow us to add and modify the behavior of elements. Using directives in angular we can modify the DOM (Document Object Module) styles, handle user functionality, and much more. Table of Content Custom DirectivesSteps to create the DirectiveBene
3 min read