20
loading...
This website collects cookies to deliver better user experience
ngIf
directive.*
*ngIf
structural-directive-demo
.myAge
and assign the value 18
to it.export class StructuralDirectiveDemoComponent implements OnInit {
myAge = 18;
constructor() { }
ngOnInit(): void { }
}
<p *ngIf="myAge >= 18">I am an Adult!</p>
<p *ngIf="myAge < 18">I am a CHILD</p>
myAge
is holding the value 18 (the model).<p *ngIf="myAge >= 18">I am an Adult!</p>
myAge >= 18
returns true
which is assigned to the directive ngIf. Since true is assigned to the directive ngIf
the p
tag is visible.myAge
variable value to say 17 so that the above condition is false but the second line of code <p *ngIf="myAge < 18">I am a CHILD</p>
returns true.p
tag is not shown/ hidden while the second p
tag is shown which was hidden when the model value was 18.checkAge() {
if (this.myAge >= 18) {
return true;
} else {
return false;
}
}
<p *ngIf="checkAge()">I am an Adult!</p>
false
, null
, undefined
, empty string
when assigned to ngIf
will result in hiding the element.