
Directives are ways provided by Angular操作DOM . Instructions are divided into属性指令and结构指令.
Attribute directive: Modify the appearance or behavior of an existing element, wrap it with [] .
Structural instructions: add and delete DOM nodes to modify the layout, use * as the instruction prefix. [Recommended related tutorials: "angular tutorial"]
1. Built-in instruction
1.1 *ngIf
渲染DOM nodes or移除DOM nodes based on conditions.
<div *ngIf="data.length == 0">No more data</div>
<div *ngIf="data.length > 0; then dataList else noData"></div> <ng-template #dataList>Course List</ng-template> <ng-template #noData>No more data</ng-template>
ng-template is used to define templates. After using ng-template to define a template, you can use ng-container and templateOutlet instructions to use it.
<ng-template #loading> <button (click)="login()">login</button> <button (click)="sigup()">sigup</button> </ng-template> <ng-container *ngTemplateOutlet="loading"> </ng-container>
1.2 [hidden]
显示DOM nodes or隐藏DOM nodes (display) according to conditions.
<div [hidden]="data.length == 0">Course List</div> <div [hidden]="data.length > 0">No more data</div>
1.3 *ngFor
traverses data to generate HTML structure
interface List {
id: number
name: string
age: number
}
list: List[] = [
{ id: 1, name: "Zhang San", 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. Custom 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"
// Receive parameter type interface Options {
bgColor?: string
}
@Directive({
selector: "[appHover]"
})
export class HoverDirective implements AfterViewInit {
//Receive parameters @Input("appHover") appHover: Options = {}
// DOM node element to be operated on: HTMLElement
// Get the DOM node to be operated on constructor(private elementRef: ElementRef) {
this.element = this.elementRef.nativeElement
}
// Set the background color of the element after the initial completion of the component template ngAfterViewInit() {
this.element.style.backgroundColor = this.appHover.bgColor || "skyblue"
}
// Add a mouse move event to the element @HostListener("mouseenter") enter() {
this.element.style.backgroundColor = "pink"
}
//Add mouse out event for element @HostListener("mouseleave") leave() {
this.element.style.backgroundColor = "skyblue"
}
} The role of the pipeline is格式化组件模板数据.
1. Built-in pipeline
date date format
currency currency format
uppercase Convert to
uppercase lowercase Convert to lowercase
json Format json data
{{ date | date: "yyyy-MM-dd" }} 2. Custom pipeline
requirements: the specified string cannot exceed Specified length
<!-- This is a... -->
{{'This is a test' | 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
]
});