ngIf is an Angular structural directive that adds or removes part of the DOM according to a template expression. It commonly gates validation messages and other conditional UI. section 10.1.1
Standalone components can import NgIf directly. Angular v15 made CommonModule entities standalone, avoiding the broader CommonModule import when a component needs only this directive. section 2.3.1
Login validation message
A standalone login component can display a warning while either credential is empty: <span class="warning" *ngIf="!credentials.email || !credentials.password">Please fill in all the required fields</span>. Its component metadata imports both FormsModule and NgIf.
Use ngIf with these practical considerations:
- Import NgIf directly in a standalone component when the template uses *ngIf; importing CommonModule also works but brings unrelated entities. section 2.3.1
- Treat nested ng-template and else chains as a readability and maintenance concern when conditions grow beyond a simple branch. section 10.1.1
- Prefer Angular’s built-in @if and @else control-flow syntax for newer templates, especially when expressing multiple branches. section 10.1.1
