Table of Contents
What will you learn
What are Angular structural directives?
How do Angular structural directives work?
Example of structural directive
How to use the *ngIf command
How to use the *ngFor command
How to use*ngSwitch Command
When do we need to use structural directives in Angular?
Summary
Home Web Front-end JS Tutorial What are structural directives in Angular? how to use?

What are structural directives in Angular? how to use?

Aug 24, 2022 pm 07:27 PM
angular angular.js

This article will take you to understand the structural instruction mode in Angular, and introduce what the structural instruction is and how to use it. I hope it will be helpful to everyone!

What are structural directives in Angular? how to use?

In Angular, there are two types of directives. Attribute directivesModify the appearance or behavior of the DOM element. Structural directivesAdd or remove DOM elements.

Structural directives are one of the most powerful features in Angular, yet they are frequently misunderstood.

If you are interested in learning structural directives, then let’s continue reading now and understand what they are, what they are used for and how to use them in your projects. [Related tutorial recommendations: "angular tutorial"]

What will you learn

In this article, you will learn aboutAngular Knowledge points of structural directive pattern. You'll know what they are and how to use them.

After studying this article, you will better understand these instructions and use them in actual projects.

What are Angular structural directives?

Angular Structural directives are directives that change the structure of the DOM. These instructions can add, remove or replace elements. Structural directives have the * symbols before their names.

In Angular, there are three standard structured directives.

  • *ngIf - Conditionally include a template (i.e. conditional rendering template) based on the Boolean value returned by the expression
  • *ngFor - Traverse the array
  • *ngSwitch - Render each matching is graph

Below? is an example of a structured directive. The syntax looks like this:

 <element ng-if="Condition"></element>
Copy after login

The conditional statement must be true or false.

<div *ngIf="worker" class="name">{{worker.name}}</div>
Copy after login

Angular Generates an element with <ng-template> and then applies the *ngIf directive. This converts it to a property binding within square brackets [], such as [ngIf]. The remainder of <div>, including the class name, is inserted into <ng-template>. For example:

<ng-template [ngIf]="worker">
  <div class="name">{{worker.name}}</div>
</ng-template>
Copy after login

How do Angular structural directives work?

To use structural directives, we need to add an element with the directive in the HTML template. Then add, delete or replace elements based on the conditions or expressions we set in the directive.

Example of structural directive

We add some simple HTML code.

app.component.html The file content is as follows:

<div style="text-align:center">
  <h1>
    Welcome 
  </h1>
</div>
<h2> <app-illustrations></app-illustrations></h2>
Copy after login

How to use the *ngIf command

We use *ngIf to determine whether to display or remove an element based on conditions. ngIf is very similar to if-else.

The *ngIf directive removes the HTML element when the expression is false. When true, a copy of the element will be added to the DOM.

The complete *ngIf code is as follows:

<h1>
	<button (click)="toggleOn =!toggleOn">ng-if illustration</button>
  </h1>
  <div *ngIf="!toggleOn">
  <h2>Hello </h2>
  <p>Good morning to you,click the button to view</p>
  </div>
  <hr>
  <p>Today is Monday and this is a dummy text element to make you feel better</p>
  <p>Understanding the ngIf directive with the else clause</p>
Copy after login

How to use the *ngFor command

us Use the *ngFor directive to iterate over the array. For example:

<ul>

    <li *ngFor="let wok of workers">{{ wok }}</li>

</ul>
Copy after login

Our componentTypeScript File:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-illustrations',
  templateUrl: './illustrations.component.html',
  styleUrls: ['./illustrations.component.css']
})
export class IllustrationsComponent implements OnInit {
  workers: any = [

    'worker 1',

    'worker 2',

    'worker 3',

    'worker 4',

    'worker 5',

  ]

  constructor() { }

  ngOnInit(): void {
  }

}
Copy after login

How to use*ngSwitch Command

Translator added: This command is very useful in actual development

We use ngSwitch to decide which element to render based on different conditional statements. The *ngSwitch directive is very similar to the switch statement we use. For example:

<div [ngSwitch]="Myshopping">
  <p *ngSwitchCase="&#39;cups&#39;">cups</p>
  <p *ngSwitchCase="&#39;veg&#39;">Vegetables</p>
  <p *ngSwitchCase="&#39;clothes&#39;">Trousers</p>
  <p *ngSwitchDefault>My Shopping</p>
</div>
Copy after login

In typescript:

Myshopping: string = '';
Copy after login

We have a MyShopping variable which has a default value for rendering in the module A specific element that satisfies the condition.

When the condition value is true, the relevant elements will be rendered into DOM, and the remaining elements will be ignored. If no element matches, the element of *ngSwitchDefault is rendered into DOM.

When do we need to use structural directives in Angular?

If you want to add or remove an element from DOM, you should use the structure directive. Of course, we can also use them to change element CSS styles, or add event listeners. You can even use them to create a new element that didn't exist before.

The best rule is: When we are thinking about manipulating the DOM, then it is time to use structural directives.

Summary

Structural directives are an important part of Angular and we can use them in many ways.

I hope that through this article, readers can better understand how to use these instructions and when to use these modes.

This article is a translation, in the form of free translation.

Original address: https://www.freecodecamp.org/news/angular-structural-directive-patterns-what-they-are-and-how-to-use-them/

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of What are structural directives in Angular? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Angular learning talks about standalone components (Standalone Component) Angular learning talks about standalone components (Standalone Component) Dec 19, 2022 pm 07:24 PM

This article will take you to continue learning angular and briefly understand the standalone component (Standalone Component) in Angular. I hope it will be helpful to you!

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

Angular + NG-ZORRO quickly develop a backend system Angular + NG-ZORRO quickly develop a backend system Apr 21, 2022 am 10:45 AM

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

See all articles