Structural Directives in Angular
Last Updated :
31 Jul, 2020
Structural directives are responsible for the Structure and Layout of the DOM Element. It is used to hide or display the things on the DOM. Structural Directives can be easily identified using the '*'. Every Structural Directive is preceded by a '*' symbol.
Some of the Build in Structural Directives with Examples are as follows:
1. *ngIf:
ngIf is used to display or hide the DOM Element based on the expression value assigned to it. The expression value may be either true or false.
Syntax:
<div *ngIf="boolean"> </div>
In the above Syntax, boolean stands for either true or false value. Hence, it leads to 2 valid syntaxes as below :
<div *ngIf="true"> </div>
<div *ngIf="false"> </div>
Example of *ngIf:
html
<div *ngIf="false">
This text will be hidden
<h1 [ngStyle]="{'color':'#FF0000'}">
GFG Structural Directive Example
</h1>
</div>
<div *ngIf="true">
This text will be displayed
<h1 [ngStyle]="{'color':'#00FF00'}">
GFG Structural Directive Example
</h1>
</div>
Output:
*ngIf Example
2. *ngIf-else:
ngIf-else works like a simple If-else statement, wherein if the condition is true then 'If' DOM element is rendered, else the other DOM Element is rendered. Angular uses ng-template with element selector in order to display the else section on DOM.
Syntax:
<div *ngIf="boolean; else id_selector"> </div>
<ng-template #id_selector> </ng-template>
In the above Syntax, boolean stands for either true or false value. If the boolean value is true then Element in If is rendered on the DOM, else another element is rendered on the DOM .
Example of *ngIf- else:
html
<div *ngIf="false;else id_selector">
This text will be hidden
<h1 [ngStyle]="{'color':'#FF0000'}">
GFG Structural Directive
If Part
</h1>
</div>
<ng-template #id_selector>
This text will be displayed
<h1 [ngStyle]="{'color':'#00FF00'}">
GFG Structural Directive
Else Part
</h1>
</ng-template>
Output:
*ngIf - else Example
3. *ngFor:
*ngFor is used to loop through the dynamic lists in the DOM. Simply, it is used to build data presentation lists and tables in HTML DOM.
Syntax:
<div *ngFor="let item of item-list"> </div>
Example of *ngFor:
Consider that you are having a list as shown below:
items = ["GfG 1", "GfG 2", "GfG 3", "GfG 4"];
html
<div *ngFor="let item of items">
<p > {{item}} </p>
</div>
Output:
*ngFor example
Example-2 of *ngFor with Indexes:
Consider that you are having a list as shown below :
items = ["GfG ", "GfG ", "GfG ", "GfG "];
html
<div *ngFor="let item of items;let i=index">
<p > {{item}} {{i}} </p>
</div>
Output:
Here, the index starts from '0' and not '1'
*ngFor with Indexes
4. *ngSwitch :
ngSwitch is used to choose between multiple case statements defined by the expressions inside the *ngSwitchCase and display on the DOM Element according to that. If no expression is matched, the default case DOM Element is displayed.
Syntax:
<div [ngSwitch]="expression">
<div *ngSwitchCase="expression_1"></div>
<div *ngSwitchCase="expression_2"></div>
<div *ngSwitchDefault></div>
</div>
In the above syntax, the expression is checked with each case and then the case matching with the expression is rendered on DOM else the Default case is rendered on the DOM.
Example of *ngSwitch:
html
<div [ngSwitch]="'one'">
<div *ngSwitchCase="'one'">One is Displayed</div>
<div *ngSwitchCase="'two'">Two is Displayed</div>
<div *ngSwitchDefault>Default Option is Displayed</div>
</div>
In the above example, the expression 'one' in ngSwitch is matched to the expression in ngSwitchCase. Hence, the Element displayed on DOM is " One is Displayed ".
Output:
ngSwitch Case example
Similar Reads
Angular 17 Structural Directives
Structural directives in Angular 17 are a special type of directives that allow you to dynamically manipulate the DOM (Document Object Model) by adding, removing, or replacing elements based on certain conditions. These directives are prefixed with an asterisk (*) in the template syntax. In this art
5 min read
Built-in Structural Directives in Angular
Directives in Angular are nothing but the classes that allow us to add and modify the behavior of elements. Using directives in angular we can modify the DOM (Document Object Module) styles, handle user functionality, and much more. In this article, we will see more about Built-in structural directi
3 min read
Recursion in Angular Directives
Recursion in Angular directives refers to the ability of a directive to include instances of itself within its template. This allows for the creation of nested structures and dynamic rendering based on the data. Recursive directives are useful when dealing with hierarchical data or tree-like structu
4 min read
Angular 10 NgPlural Directive
In this article, we are going to see what is NgPlural in Angular 10 and how to use it. The NgPlural in Angular10 is used to Add or remove DOM sub-trees based on a numeric value. Syntax: <li *NgPlural='condition'></li> NgModule: Module used by NgPlural is: CommonModule  Selectors: [NgPlu
1 min read
AngularJS ng-value Directive
The ng-value Directive in AngularJS is used to specify the value of an input element. This directive can be used to achieve the one-way binding for the given expression to an input element, especially when the ng-model directive is not used for that specific element. It is supported by <input>
2 min read
What are Directives in AngularJS ?
AngularJS directives are extended HTML attributes having the prefix ng-. Directives are markers on the DOM element which tell Angular JS to attach a specified behavior to that DOM element or even transform the DOM element with its children. During compilation, the HTML compiler traverses the DOM mat
7 min read
Angular10 NgPluralCase Directive
In this article, we are going to see what is NgPluralCase in Angular 10 and how to use it. The NgPluralCase in Angular10 is used to create a view that will be added or removed from the parent NgPlural when the given expression matches the plural expression. We can use the values to make the output p
1 min read
Built-in directives in Angular
Directives are markers in the Document Object Model(DOM). Directives can be used with any controller or HTML tag which will tell the compiler what exact operation or behavior is expected. There are some directives present that are predefined but if a developer wants he can create new directives (cus
5 min read
AngularJS ng-init Directive
The ng-init Directive is used to initialize AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application. Syntax: <element ng-init = "expression"
1 min read
Angular10 NgFor Directive
In this article, we are going to see what is NgFor in Angular 10 and how to use it. NgFor is used as a structural directive that renders each element for the given collection each element can be displayed on the page. Syntax: <li *ngFor='condition'></li> NgModule: Module used by NgForOf
1 min read