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!
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: 'skyblue' }">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" } }
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
{{ date | date: "yyyy-MM-dd" }}
2. Custom pipeline
Requirement: The specified string cannot exceed the specified length<!-- 这是一... --> {{'这是一个测试' | summary: 3}}
// summary.pipe.ts import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'summary' }); export class SummaryPipe implements PipeTransform { transform (value: string, limit?: number) { if (!value) return null; let actualLimit = (limit) ? limit : 50; return value.substr(0, actualLimit) + '...'; } }
// app.module.ts import { SummaryPipe } from './summary.pipe' @NgModule({ declarations: [ SummaryPipe ] });
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!