ngIf Directive is the part of structural directives.The NgIf is the most straightforward structural Directive and comfortable to understand. The ngIf Directives is used to add and remove HTML elements according to the expression. The expression returns a Boolean value. If the appearance is false, then the removed, otherwise portion is inserted.

It is same as the ng-if Directive of AngularJS. The ngIf directive doesn’t hide elements. It adds and removes them physically from the DOM. We can confirm it by using browser developer tools to inspect the DOM. When the condition is false, NgIf removes the host element from the DOM, which detection, and destroys it.

ngIf Syntax

The *ngIf directive form with an “else” block

Content to render when condition is true.

The ngIf directive does not hide the DOM element. It removes the entire element along with its sub tree from the DOM. It also removes the state freeing up the resources attached to the element.

The *ngIf directive is most commonly used to conditionally show an inline template. See the following example:

Same template example with else block

Pin It on Pinterest

Share This