The Wayback Machine - https://web.archive.org/web/20240928070435/https://www.geeksforgeeks.org/structural-directives-in-angular/
Open In App

Structural Directives in Angular

Last Updated : 31 Jul, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:




<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:




<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"];




<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 "];




<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:




<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 article, we will learn more about Structural Directiv
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 directives in Angular. Structural Directives In Angular,
3 min read
Difference between structural and attribute directives
Structural directives manipulate the DOM layout by adding, removing, or replacing elements, while attribute directives modify the appearance or behavior of elements without affecting their structure. 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
5 min read
Angular 7 | Directives
Directives in Angular 7 are Typescript class which is declared with decorator @Directive. These are the Document Object Model (DOM) instruction sets, which decide how logic implementation can be done.Angular directives can be classified into three types: Component Directives: It forms the main class and is declared by @Component. It contains the de
3 min read
Angular PrimeNG Directives Complete Reference
Angular PrimeNG Directives provides various components, such as defer, Focus Trap, StyleClass, & Ripple, that facilitate the different effects on the component, while loading the content, along with including the animation styling to make interactive web applications, along with enhancing the user experience. The complete list of Directives Com
3 min read
How to pass multiple parameter to @Directives in Angular ?
Angular directives are TypeScript classes decorated with the @Directive decorator. These are powerful tools for manipulating the DOM and adding behavior to elements. They can modify the behavior or appearance of DOM elements within an Angular application. Directives can be broadly categorized into three types: Component Directives: These are direct
3 min read
Use of *ngIf and *ngFor Directives in Angular
Angular is a very popular framework for building scalable web applications. It provides many features and tools to make the development process smooth. Two important directives in Angular are *ngIf and *ngFor. *ngIf is used to conditionally render HTML elements, while *ngFor is used to iterate over a collection of data. In this article, we will lea
5 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 (custom-directive). There are basically 3 types of dir
5 min read
Attribute Directives in Angular
Attribute directives are a powerful tool that allows you to manipulate the behavior and appearance of HTML elements. In this article, you will see the fundamentals of attribute directives. Table of Content What are Attribute Directive?Benefits of Attribute DirectiveTypes of Attribute DirectivesSteps to create Custom DirectivesExample of Attribute D
5 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 structures. Creating Angular application & module ins
4 min read
How To Share Data Between Child And Parent Directives And Components In Angular?
Sharing data between child and parent components in Angular is important for building dynamic and interactive applications. This can be done through various concepts, including input and output bindings or using a shared service. This article will explore how to use an Angular service to enable data communication, allowing both parent and child com
5 min read
TypeScript Structural Typing
TypeScript type system is based on structural typing, therefore, TypeScript’s type compatibility is determined by the structure (i.e., the properties and methods) rather than explicit declarations or names. In this article, we will learn in detail about TypeScript Structural Typing. What is Structural Typing?Structural typing is a type system in wh
6 min read
Directives in JSP
JSP directives are the elements of a JSP source code that guide the web container on how to translate the JSP page into its respective servlet. Syntax : <%@ directive attribute = "value"%> Directives can have a number of attributes that you can list down as key-value pairs and separate by commas. The blanks between the @ symbol and the direct
4 min read
What is the difference between ng-if and data-ng-if directives ?
The ng-if is a directive in AngularJS which is used to remove the HTML element if the value of the expression or variable is false, unlike ng-hide which just hides the HTML element from the DOM. Syntax: <element angular_directive=expression> Contents... </element> There are few other options which behave like ng-if. There is no differen
2 min read
What is the role of ng-app, ng-init and ng-model directives in AngularJS ?
In this article, we will learn about the directives in AngularJS & explore the roles of various directives such as ng-app, ng-init & ng-model in angularJS. The AngularJS directive is a command that gives HTML documents new functionality. When we use the AngularJS directives, it will first find all the directives in the HTML document and the
2 min read
What is the use @error and @debug directives in SASS ?
In this article, we will see the use of @error and @debug directives in SASS. Sass is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. so that we can reuse the code in multiple elements. The @error directive Sass consists of three directives that are used for pro
2 min read
Vue.js Custom Directives with Function Shorthand
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
3 min read
Vue.js Custom Directives with Components
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
3 min read
Vue.js Custom Directives with Object Literals
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
3 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 matching directives against the DOM elements. When we
7 min read
What are triple-slash Directives in TypeScript ?
Triple-slash directives in TypeScript are special comments that are used to provide instructions to the TypeScript compiler (tsc) or the IDE (Integrated Development Environment) about how to process a TypeScript file. These directives start with three forward slashes (///) and are typically placed at the top of a TypeScript file. Triple-slash direc
6 min read
AngularJS Directives Complete Reference
Directives are markers in the Document Object Model(DOM). Directives can be used with any of controller or HTML tag which will tell the compiler what exact operation or behavior is expected. There are some directives present which is predefined but if a developer wants he can create new directives (custom-directive). Example: This example uses ng-a
2 min read
Difference between directives and components
In Angular, directives, and components are essential concepts that help to build dynamic and interactive web applications. While they both play significant roles, understanding their differences and use cases is crucial for effective Angular development. DirectivesDirectives in Angular are instructions in the DOM that tell Angular how to manipulate
4 min read
AngularJS Directives
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 (custom-directive). List of Directives:The following t
9 min read
Routing in Angular JS using Angular UI Router
AngularJS is a front-end web application framework based on JavaScript and is maintained by Google. AngularJS interprets the attributes of HTML as directives to bind input/output components to a model represented by standard JavaScript variables. Pre-requisites: HTML CSS JavaScript AngularJS Angular-UI-Router is an AngularJS module used to create r
3 min read
Angular 7 | Angular Data Services using Observable
Observables Observable manage async data and a few other useful patterns. Observables are similar to Promises but with a few key differences. Unlike Promises, Observables emit multiple values over time. In real scenarios, web socket or real-time based data or event handlers can emit multiple values over any given time. In such a case Observables ar
4 min read
Adding Angular Material Component to Angular Application
Angular Material provides Design Components for Angular. This is build and maintained by Angular Team. This provides the Angular developers with common UI components and tools to help build their own custom components. We as a user can integrate this with our Angular Application using the npm packages provided by them. Angular Material also provide
2 min read
Difference between Angular 4 and Angular 5
Angular 4: Angular 4 was launched 5 years after the official release of AngularJS. Angular 4 is a JavaScript-based open-source framework for building web applications in JavaScript, HTML, and TypeScript, which is a superset of JavaScript. Angular 4 was launched in the March 2017 and supports earlier TypeScript versions such as TypeScript 2.2 and 2.
2 min read
Angular Cheat Sheet - A Basic Guide to Angular
Angular is a client-side TypeScript-based, front-end web framework developed by the Angular Team at Google, that is mainly used to develop scalable single-page web applications(SPAs) for mobile & desktop. Angular is a great, reusable UI (User Interface) library for developers that helps in building attractive, steady, and utilitarian web pages
15+ min read
How to use angular-calendar in Angular 17?
Angular applications often require scheduling and event management features, which can be challenging to implement from scratch. Fortunately, Angular Calendar, a powerful library built specifically for Angular, provides the solution to this problem. In this article, we'll explore how Angular Calendar can be used in the Angular Project. Prerequisite
2 min read