I'm starting my studies in Angular 7 and in a lesson on Structural Directives, I learned that I can add or remove elements HTML
of DOM
.
I understand that there are 3 main , ngIf
, ngSwitch
and ngFor
.
The ngIf
and ngSwitch
are used to conditionally rotate the HTML
elements.
ngFor
is used to render a list of HTML
elements.
Using ngIf
, I have an example:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-test',
//templateUrl: './test.component.html',
template: '
<h2 *ngIf="true">
Marcielli
</h2>
',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
NOTE: For study purpose, I am using the same file.
When ngIf
is set to true
, the screen looks like this:
Andwhensettofalse
,itlookslikethis:
The question is: In what real situation would I use this? For what reason, for example, would I want to remove an element from the DOM?