Home > Web Front-end > JS Tutorial > Let's talk about directives in Angular

Let's talk about directives in Angular

青灯夜游
Release: 2021-06-17 10:25:18
forward
2710 people have browsed it

This article will introduce to you the directives in Angular. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Let's talk about directives in Angular

Environment:

  • Angular CLI: 11.0.6
  • Angular: 11.0.7
  • Node: 12.18.3
  • npm : 6.14.6
  • IDE: Visual Studio Code

1. Summary


Directives were very popular in the Angular 1.0 era (called AngularJS at the time), but are less used now. It can be simply understood that instructions (Directives) are used to extend existing Elements (DOM). [Related tutorial recommendations: "angular tutorial"]

2. The relationship between components and instructions


If If you look at the Angular source code, you can see the following definition

/**
 * Supplies configuration metadata for an Angular component.
 *
 * @publicApi
 */
export declare interface Component extends Directive {
Copy after login

Yes, Component is derived from Directive, that is to say, Component belongs to Directive.

2.1. Types of instructions

  • Component is a sub-interface of Directive and a special instruction. Component It can have HTML templates, but Directive cannot have templates.

  • Attribute-type directives: used to modify the appearance and behavior of DOM elements, but will not change the DOM structure. Typical attribute-type directives in Angular's built-in instructions include ngClass and ngStyle. If you plan to encapsulate Your own component library, attribute-type instructions are necessary.

  • Structural instructions: You can modify the DOM structure. The built-in common structural instructions include *ngFor, *ngIf and * ngSwitch. Since structural directives modify the DOM structure, multiple structural directives cannot be used on the same HTML tag at the same time. If you want to use multiple structural directives on the same HTML element, you can consider adding an empty layer of elements to nest them, such as an empty layer outside (p).

3. The purpose of instructions in Angular


Instructions are used in Angularr to enhance the functionality of DOM, including HTML native DOM and our Your own custom component. For example, you can extend a Button to avoid secondary clicks before the server responds after clicking; highlight certain income content, etc.

4. Command examples


##4.1. Command function

Implement a command. When the mouse moves over it, the background displays in yellow. When the mouse is moved away, it returns to normal.

4.2. Anuglar CLI generates basic files

ng generate directive MyHighlight
Copy after login

Anuglar CLI automatically generates html, css, ut and other files.

4.3. Directive instruction core code

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.backgroundColor = 'yellow';
    }

    @HostListener('mouseenter') onMouseEnter() {
        this.highlight('yellow');
    }

    @HostListener('mouseleave') onMouseLeave() {
        this.highlight(null);
    }

    private highlight(color: string) {
        this.el.nativeElement.style.backgroundColor = color;
    }
}
Copy after login

4.4. Use this instruction

<p my-highlight>Highlight me!</p>
Copy after login

my-highlight is our element extended attribute (instruction, directive).

5. Summary


  • Directive is used to extend the functionality of DOM elements or components.

  • *ngFor, *ngIf and *ngSwitch in Angular are all built-in instructions in Angular.

For more programming-related knowledge, please visit:

Programming Video! !

The above is the detailed content of Let's talk about directives in Angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template