Angular 8 ngIf Directive
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
Condition is true and ngIf is true.
Condition is false and ngIf is false.
The *ngIf directive form with an “else” block
Content to render when condition is true.
Content to render when condition is false.
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:
@Component({ selector: ‘ng-if-simple’, template: show= {[show}}
Text to show` }) export class NgIfSimple { show: boolean =true; }
Same template example with else block
@Component({ selector :’ng-if-else’; template: show={{show}}
Text to showAlternate text while primary text is hidden ` }] export class NgIfElse{ show: boolean = true; }