Concept Flow - *ngIf for conditional rendering
Start
Evaluate *ngIf condition
Render element in DOM
Do not render element
End
Angular checks the *ngIf condition. If true, it adds the element to the page. If false, it removes or skips it.
<div *ngIf="showMessage">Hello, Angular!</div> <button (click)="toggle()">Toggle Message</button> showMessage = true; toggle() { this.showMessage = !this.showMessage; }
| Step | Action | *ngIf Condition (showMessage) | Render Element? | DOM Change |
|---|---|---|---|---|
| 1 | Initial render | true | Yes | Element <div>Hello, Angular!</div> added to DOM |
| 2 | User clicks button | true -> false | No | Element removed from DOM |
| 3 | User clicks button again | false -> true | Yes | Element added back to DOM |
| 4 | No further clicks | true | Yes | No change |
| Exit | No more actions | - | - | Rendering stable |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| showMessage | true | false | true | true | true |
*ngIf directive conditionally adds or removes elements from the DOM. Syntax: <element *ngIf="condition">content</element> If condition is true, element is rendered. If false, element is removed completely. Useful for showing/hiding UI based on variables.