search
HomeWeb Front-endJS TutorialDo you know Directives in Angular? Here are detailed explanations of the three directive instructions of angularjs

This article mainly introduces the three instruction types in angularjs. Which three types are they? Let this article tell you, and how to use these three types. It's all here. Now let's take a look

In Angular2 there are three types of directives (Directive), as follows:
1 . Attribute directives - directives that change the display and behavior of elements. For example: NgStyle …
2. Structural directives - directives that change the structure of the DOM by adding and removing DOM elements. For example: NgFor, NgIf …
3. Component — A directive that owns a template.

1. Attribute instructions (ngStyle, ngClass)

NgStyle
Bind an object with a shape such as CSS property name: value, The value is the specific css style, eg:

<p></p><p></p>

Note, in the description of ngStyle, we use single quotes for background-color, but not for color. Why is this? Because the parameter of ngStyle is a JavaScript object, and color is a legal key, no quotes are needed. But in background-color, hyphens are not allowed to appear in the key name of an object unless it is a string, so quotes are used. In general, try not to use quotes around object keys unless absolutely necessary.

//Dynamic use

<span>{{ color }} text</span>

//Judgment addition

<p></p><p></p>

NgClass
Use the NgClass command to add or remove multiple ones at the same time kind. NgClass binds an object with a shape such as CSS class name: value, where the value is a Boolean value. When the value is true, a template element of the corresponding type is added, otherwise it is removed.

//Basic usage

 <p>此时p不包含bordered 类名</p>
 <p>此时p含有bordered 类名</p>

//Judgment

 <i></i>

Do you know Directives in Angular? Here are detailed explanations of the three directive instructions of angularjs

Do you know Directives in Angular? Here are detailed explanations of the three directive instructions of angularjs

##2. Structural directives (ngIf, ngFor, ngSwitch)

NgIfSpecifies binding a Boolean expression , when the expression returns true, an element and its sub-elements can be added to the DOM tree node, otherwise they are removed.

If the result of the expression returns a false value, the element will be removed from the DOM.

Here are some examples:

<p></p> //不显示
<p> b"></p>//
<p></p> 
<p></p>

NgForThe NgFor instruction can perform certain operations repeatedly to display data. The NgFor directive supports an optional index.
The syntax is *ngFor="let item of items":
The let item syntax specifies a (template) variable that receives each element in the items array.
items is a collection of items from the component controller

this.cities = ['厦门', '福州', '漳州'];
<p>{{ c }}</p>
Get the index

When iterating through the array, we may also want to get the index of each item.
We can insert the syntax let idx = index into the value of the ngFor directive and separate it with a semicolon, so that we can get the index.

<p>{{ num+1 }} . {{ c }}</p>
The results are as follows:

1.Xiamen
2.Fuzhou
3.Zhangzhou

ngSwitchSometimes you need to follow a Render different elements given the conditions.
When encountering this situation, you may use ngIf multiple times like this:

<p>
    </p><p>Var is A</p>
    <p>Var is B</p>
    <p>Var is C</p>
    <p>Var is something else</p>
For this situation, Angular introduces the ngSwitch directive. (If you want to learn more, go to the PHP Chinese website

AngularJS Development Manual to learn)

NgSwitch: Bind to a value expression that returns a control condition

NgSwitchCase: Bind to a Returns the value expression that matches the condition
NgSwitchDefault: An attribute used to mark the default element, which is optional. If we don't use it, nothing will be rendered if myVar doesn't match any of the expected values ​​
.
Use the ngSwitch directive to rewrite the above example:

<p>
    </p><p>Var is A</p>
    <p>Var is B</p>
    <p>Var is C</p>
    <p>Var is something else</p>
3. Component
The creation of attribute directives requires at least one decorated with @Directive decorator controller class. The @Directive decorator specifies a selector name that indicates the name of the attribute associated with this directive. <p></p>Next, start creating a simple attribute-type directive. The function of this directive is to obtain the minimum height of .quotation-area when the user-quotation-view.component.html page is refreshed. <p></p>1. First, we confirm the command name, quotationArea<p></p>
<p></p>
Apply this command as an attribute to a DOM element, that is, we need to find a host element for this command. .

2. Then we create a quotationArea.directive.tss file with the following code structure:

import {Component, Directive, ElementRef, OnInit} from '@angular/core';
@Directive({ selector: '[quotationArea]'})
export class QuotationAreaDirective implements OnInit {

  el:ElementRef;
  constructor(el: ElementRef) {
    this.el = el;
  }
  ngOnInit() {
    const $el = $(this.el.nativeElement);
    const windowHeight = document.documentElement.clientHeight; //获取窗口高度
    const bar=document.getElementsByClassName('bar-nav')[0]
    const barHeight =bar.clientHeight;
    const heightValue=windowHeight - barHeight;
    $el.css('height',(heightValue) + 'px');

  }
}
3. Next, we need to explicitly declare our own defined directives in module.ts so that When Angularr parses the template, it can correctly identify an instruction we set ourselves. <p></p>
import {QuotationAreaDirective} from './user-quotation/user-quotation-view/quotationArea.directive';
    declarations: [QuotationAreaDirective]
The result is as shown:<p></p>

Do you know Directives in Angular? Here are detailed explanations of the three directive instructions of angularjs

Okay, this article ends here (if you want to see more, go to the PHP Chinese website AngularJS User Manual to learn ), if you have any questions, you can leave a message below


The above is the detailed content of Do you know Directives in Angular? Here are detailed explanations of the three directive instructions of angularjs. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Angular学习之聊聊独立组件(Standalone Component)Angular学习之聊聊独立组件(Standalone Component)Dec 19, 2022 pm 07:24 PM

本篇文章带大家继续angular的学习,简单了解一下Angular中的独立组件(Standalone Component),希望对大家有所帮助!

angular学习之详解状态管理器NgRxangular学习之详解状态管理器NgRxMay 25, 2022 am 11:01 AM

本篇文章带大家深入了解一下angular的状态管理器NgRx,介绍一下NgRx的使用方法,希望对大家有所帮助!

项目过大怎么办?如何合理拆分Angular项目?项目过大怎么办?如何合理拆分Angular项目?Jul 26, 2022 pm 07:18 PM

Angular项目过大,怎么合理拆分它?下面本篇文章给大家介绍一下合理拆分Angular项目的方法,希望对大家有所帮助!

聊聊自定义angular-datetime-picker格式的方法聊聊自定义angular-datetime-picker格式的方法Sep 08, 2022 pm 08:29 PM

怎么自定义angular-datetime-picker格式?下面本篇文章聊聊自定义格式的方法,希望对大家有所帮助!

浅析Angular中的独立组件,看看怎么使用浅析Angular中的独立组件,看看怎么使用Jun 23, 2022 pm 03:49 PM

本篇文章带大家了解一下Angular中的独立组件,看看怎么在Angular中创建一个独立组件,怎么在独立组件中导入已有的模块,希望对大家有所帮助!

手把手带你了解Angular中的依赖注入手把手带你了解Angular中的依赖注入Dec 02, 2022 pm 09:14 PM

本篇文章带大家了解一下依赖注入,介绍一下依赖注入解决的问题和它原生的写法是什么,并聊聊Angular的依赖注入框架,希望对大家有所帮助!

Angular的:host、:host-context、::ng-deep选择器Angular的:host、:host-context、::ng-deep选择器May 31, 2022 am 11:08 AM

本篇文章带大家深入了解一下angular中的几个特殊选择器:host、:host-context、::ng-deep,希望对大家有所帮助!

如何解决“[Vue warn]: Failed to resolve directive”错误如何解决“[Vue warn]: Failed to resolve directive”错误Aug 20, 2023 pm 05:54 PM

如何解决“[Vuewarn]:Failedtoresolvedirective”错误Vue.js是一款流行的JavaScript框架,它提供了很多有用的功能来开发交互式的Web应用程序。其中一个特性是指令(Directive),它可以用于扩展HTML元素的功能或添加特定的行为。然而,有时候在使用指令时可能会遇到一个错误:“[Vuewarn]:F

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool