Home  >  Article  >  Web Front-end  >  Angular learning talks about instructions and pipelines

Angular learning talks about instructions and pipelines

青灯夜游
青灯夜游forward
2022-05-17 11:01:571551browse

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>
<div *ngIf="data.length > 0; then dataList else noData"></div>
<ng-template #dataList>课程列表</ng-template>
<ng-template #noData>没有更多数据</ng-template>

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>

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>

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 }
]
<li
    *ngFor="
      let item of list;
      let i = index;
      let isEven = even;
      let isOdd = odd;
      let isFirst = first;
      let isLast = last;
    "
  >
  </li>
<li *ngFor="let item of list; trackBy: identify"></li>
identify(index, item){
  return item.id; 
}

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>
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"
  }
}

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" }}

2. Custom pipeline

Requirement: The specified string cannot exceed the specified length

<!-- 这是一... -->
{{&#39;这是一个测试&#39; | summary: 3}}
// 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;;
    }
}
// app.module.ts
import { SummaryPipe } from &#39;./summary.pipe&#39;
@NgModule({
    declarations: [
      SummaryPipe
    ] 
});

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete