本篇內(nèi)容主要講解“angular指令和管道怎么用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“angular指令和管道怎么用”吧!
“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個(gè)人一直以來堅(jiān)持追求的企業(yè)文化。 成都創(chuàng)新互聯(lián)公司是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、軟件開發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺傳達(dá),提供有針對性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!

指令是 Angular 提供的操作 DOM 的途徑。指令分為屬性指令和結(jié)構(gòu)指令。
屬性指令:修改現(xiàn)有元素的外觀或行為,使用 [] 包裹。
結(jié)構(gòu)指令:增加、刪除 DOM 節(jié)點(diǎn)以修改布局,使用*作為指令前綴。
1、內(nèi)置指令
1.1 *ngIf
根據(jù)條件渲染 DOM 節(jié)點(diǎn)或移除 DOM 節(jié)點(diǎn)。
<div *ngIf="data.length == 0">沒有更多數(shù)據(jù)</div>
<div *ngIf="data.length > 0; then dataList else noData"></div> <ng-template #dataList>課程列表</ng-template> <ng-template #noData>沒有更多數(shù)據(jù)</ng-template>
ng-template 是用來定義模板的,當(dāng)使用 ng-template 定義好一個(gè)模板之后,可以用 ng-container 和 templateOutlet 指令來進(jìn)行使用。
<ng-template #loading> <button (click)="login()">login</button> <button (click)="sigup()">sigup</button> </ng-template> <ng-container *ngTemplateOutlet="loading"> </ng-container>
1.2 [hidden]
根據(jù)條件顯示 DOM 節(jié)點(diǎn)或隱藏 DOM 節(jié)點(diǎn) (display)。
<div [hidden]="data.length == 0">課程列表</div> <div [hidden]="data.length > 0">沒有更多數(shù)據(jù)</div>
1.3 *ngFor
遍歷數(shù)據(jù)生成HTML結(jié)構(gòu)
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、自定義指令
需求:為元素設(shè)置默認(rèn)背景顏色,鼠標(biāo)移入時(shí)的背景顏色以及移出時(shí)的背景顏色。
<div [appHover]="{ bgColor: 'skyblue' }">Hello Angular</div>import { AfterViewInit, Directive, ElementRef, HostListener, Input } from "@angular/core"
// 接收參的數(shù)類型
interface Options {
bgColor?: string
}
@Directive({
selector: "[appHover]"
})
export class HoverDirective implements AfterViewInit {
// 接收參數(shù)
@Input("appHover") appHover: Options = {}
// 要操作的 DOM 節(jié)點(diǎn)
element: HTMLElement
// 獲取要操作的 DOM 節(jié)點(diǎn)
constructor(private elementRef: ElementRef) {
this.element = this.elementRef.nativeElement
}
// 組件模板初始完成后設(shè)置元素的背景顏色
ngAfterViewInit() {
this.element.style.backgroundColor = this.appHover.bgColor || "skyblue"
}
// 為元素添加鼠標(biāo)移入事件
@HostListener("mouseenter") enter() {
this.element.style.backgroundColor = "pink"
}
// 為元素添加鼠標(biāo)移出事件
@HostListener("mouseleave") leave() {
this.element.style.backgroundColor = "skyblue"
}
}管道的作用是格式化組件模板數(shù)據(jù)。
1、內(nèi)置管道
date 日期格式化
currency 貨幣格式化
uppercase 轉(zhuǎn)大寫
lowercase 轉(zhuǎn)小寫
json 格式化 json 數(shù)據(jù)
{{ date | date: "yyyy-MM-dd" }}2、自定義管道
需求:指定字符串不能超過規(guī)定的長度
<!-- 這是一... -->
{{'這是一個(gè)測試' | 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
]
});到此,相信大家對“angular指令和管道怎么用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站標(biāo)題:angular指令和管道怎么用
本文地址:http://chinadenli.net/article24/ppcsce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、、網(wǎng)站內(nèi)鏈、云服務(wù)器、Google、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)