Home > Web Front-end > JS Tutorial > body text

Angular learning talks about instructions and pipelines

青灯夜游
Release: 2022-05-17 11:01:57
forward
1586 people have browsed it

This article will take you to understand the Directive and Pipe in angular, and briefly introduce the relevant knowledge points: built-in instructions and custom instructions, built-in pipes and custom pipes, I hope to be helpful!

Angular learning talks about instructions and pipelines

Directive Directive


Directive is the way provided by Angular to operate the DOM . Instructions are divided into attribute instructions and structural instructions.

  • #Attribute directive: To modify the appearance or behavior of an existing element, use [] to wrap it.

  • Structural instructions: add and delete DOM nodes to modify the layout, use * as the instruction prefix. [Related tutorial recommendations: "angular tutorial"]

1. Built-in instructions

1.1 *ngIf

Render DOM nodes or remove DOM nodes based on conditions .

<div *ngIf="data.length == 0">没有更多数据</div>
Copy after login
<div *ngIf="data.length > 0; then dataList else noData"></div>
<ng-template #dataList>课程列表</ng-template>
<ng-template #noData>没有更多数据</ng-template>
Copy after login

ng-template is used to define templates. After using ng-template to define a template, you can use ng-container and templateOutlet directives to use.

<ng-template #loading>
 <button (click)="login()">login</button>
 <button (click)="sigup()">sigup</button>
</ng-template>
<ng-container *ngTemplateOutlet="loading">
</ng-container>
Copy after login

1.2 [hidden]

Show DOM nodes or hide DOM nodes based on conditions (display).

<div [hidden]="data.length == 0">课程列表</div>
<div [hidden]="data.length > 0">没有更多数据</div>
Copy after login

1.3 *ngFor

Traverse data to generate HTML structure

interface List {
  id: number
  name: string
  age: number
}

list: List[] = [
  { id: 1, name: "张三", age: 20 },
  { id: 2, name: "李四", age: 30 }
]
Copy after login
<li
    *ngFor="
      let item of list;
      let i = index;
      let isEven = even;
      let isOdd = odd;
      let isFirst = first;
      let isLast = last;
    "
  >
  </li>
Copy after login
<li *ngFor="let item of list; trackBy: identify"></li>
Copy after login
identify(index, item){
  return item.id; 
}
Copy after login

2. Customization Command

Requirements: Set the default background color for the element, the background color when the mouse moves in, and the background color when the mouse moves out.

<div [appHover]="{ bgColor: &#39;skyblue&#39; }">Hello Angular</div>
Copy after login
import { AfterViewInit, Directive, ElementRef, HostListener, Input } from "@angular/core"

// 接收参的数类型
interface Options {
  bgColor?: string
}

@Directive({
  selector: "[appHover]"
})
export class HoverDirective implements AfterViewInit {
  // 接收参数
  @Input("appHover") appHover: Options = {}
  // 要操作的 DOM 节点
  element: HTMLElement
	// 获取要操作的 DOM 节点
  constructor(private elementRef: ElementRef) {
    this.element = this.elementRef.nativeElement
  }
	// 组件模板初始完成后设置元素的背景颜色
  ngAfterViewInit() {
    this.element.style.backgroundColor = this.appHover.bgColor || "skyblue"
  }
	// 为元素添加鼠标移入事件
  @HostListener("mouseenter") enter() {
    this.element.style.backgroundColor = "pink"
  }
	// 为元素添加鼠标移出事件
  @HostListener("mouseleave") leave() {
    this.element.style.backgroundColor = "skyblue"
  }
}
Copy after login

Pipe Pipe


The role of the pipe is to format component template data.

1. Built-in pipeline

  • date date format

  • currency Currency formatting

  • uppercase Convert to uppercase

  • lowercase Convert to lowercase

  • ##json Format json data

  • {{ date | date: "yyyy-MM-dd" }}
    Copy after login

2. Custom pipeline

Requirement: The specified string cannot exceed the specified length

<!-- 这是一... -->
{{&#39;这是一个测试&#39; | summary: 3}}
Copy after login
// summary.pipe.ts
import { Pipe, PipeTransform } from &#39;@angular/core&#39;;

@Pipe({
   name: &#39;summary&#39; 
});
export class SummaryPipe implements PipeTransform {
    transform (value: string, limit?: number) {
        if (!value) return null;
        let actualLimit = (limit) ? limit : 50;
        return value.substr(0, actualLimit) + &#39;...&#39;;
    }
}
Copy after login
// app.module.ts
import { SummaryPipe } from &#39;./summary.pipe&#39;
@NgModule({
    declarations: [
      SummaryPipe
    ] 
});
Copy after login
For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of Angular learning talks about instructions and pipelines. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!