Last Updated : 23 Jul, 2025
In this article, we are going to see what is NgIf in Angular 10 and how to use it.
The ngIf Directive in Angular10 is used to remove or recreate a portion of HTML element based on an expression. If the expression inside it is false then the element is removed and if it is true then the element is added to the DOM.
Syntax:
<li *ngIf='condition'></li>
NgModule: Module used by NgIf is:
Selectors:
Approach:
Example 1:
app.component.ts
import { Component, Inject } from '@angular/core';
import { PLATFORM_ID } from '@angular/core';
import { isPlatformWorkerApp } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
myBool = true;
}
app.component.html
<div *ngIf = 'myBool'>Boolean is set to true</div>
Output:
Example 2:
JavaScript
import { Component, Inject } from '@angular/core';
import { PLATFORM_ID } from '@angular/core';
import { isPlatformWorkerApp } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
variab = 10;
}
app.component.html
<div *ngIf = 'variab==1; else multi'>{{variab}}</div>
<ng-template #multi>
{{variab *2}}
</ng-template>
Output:
Reference: https://v17.angular.io/api/common/NgIf
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4