Angular2的管道Pipe的使用方法
管道Pipe可以將數(shù)據(jù)作為輸入,然后按照規(guī)則將其轉(zhuǎn)換并輸出。在Angular2中有許多內(nèi)置的Pipe,比如DatePipe、UpperCasePipe和CurrencyPipe等。在這里我們主要介紹如何自定義Pipe。
1. 管道定義
Pipe的定義如下代碼所示:
import { PipeTransform, Pipe } from '@angular/core';
/*users: Array<any> = [
{ name: '1', id: 1 },
{ name: '2', id: 2 },
{ name: '3', id: 3 },
{ name: '4', id: 4 },
{ name: '5', id: 5 },
{ name: '6', id: 6 }
];*/
@Pipe({ name: 'filterUser' })
export class FilterUserPipe implements PipeTransform {
transform(allUsers: Array<any>, ...args: any[]): any {
return allUsers.filter(user => user.id > 3);
}
}
如代碼所示,
- 需要使用@Pipe來裝飾類
- 實(shí)現(xiàn)PipeTransform的transform方法,該方法接受一個輸入值和一些可選參數(shù)
- 在@Pipe裝飾器中指定管道的名字,這個名字就可以在模板中使用。
注意:當(dāng)定義完成后,不要忘記在Module的declarations數(shù)組中包含這個管道。
2. 管道使用
user.template.html實(shí)現(xiàn)如下所示:
<div>
<ul>
<li *ngFor="let user of (users | filterUser)">
{{user.name}}
</li>
</ul>
</div>
<button (click)="addUser()"> add new user</button>
user.component.ts實(shí)現(xiàn)如下所示:
import { Component } from "@angular/core";
@Component({
templateUrl: './user.template.html',
})
export class EnvAppComponent {
id = 7;
users: Array<any> = [
{ name: '1', id: 1 },
{ name: '2', id: 2 },
{ name: '3', id: 3 },
{ name: '4', id: 4 },
{ name: '5', id: 5 },
{ name: '6', id: 6 }
];
addUser() {
this.users.push({ name: this.id++, id: this.id++ })
}
}
在user.component.ts中初始了數(shù)據(jù)users和定義一個添加user的方法,在user.template.html中使用自定義管道filterUser。
當(dāng)啟動應(yīng)用時,可以發(fā)現(xiàn)只有id大于3的user被顯示出來了。可是,當(dāng)你點(diǎn)擊按鈕添加用戶時,發(fā)現(xiàn)并沒有什么反應(yīng),數(shù)據(jù)并沒有改變。這是為什么呢?
3. 數(shù)據(jù)變更檢測
在Angular2中管道分為兩種:純管道和非純管道。默認(rèn)情況下管道都是純管道。
純管道就是在Angular檢測到它的輸入值發(fā)生了純變更時才會執(zhí)行管道。純變更也就是說原始數(shù)據(jù)類型(如String、Number和Boolean等)或者對象的引用發(fā)生變化。該管道會忽略對象內(nèi)部的變化,在示例中,數(shù)組的引用沒有發(fā)生改變,改變的只是數(shù)組內(nèi)部的數(shù)據(jù),所以當(dāng)我們添加數(shù)據(jù)時并沒有立即響應(yīng)在頁面上。
非純管道會在組件的變更檢測周期中執(zhí)行,而且會對對象的內(nèi)部數(shù)據(jù)進(jìn)行檢測。
在我們的示例中將管道變更為非純管道是非常賤簡單的,只要在管道元數(shù)據(jù)中將添加pure標(biāo)志為false即可。
代碼如下所示:
@Pipe({ name: 'filterUser', pure: false })
export class FilterUserPipe implements PipeTransform {
transform(allUsers: Array<any>, ...args: any[]): any {
return allUsers.filter(user => user.id > 3);
}
}
這樣每當(dāng)我們添加新用戶時,數(shù)據(jù)就會馬上響應(yīng)在頁面上了。
在根模塊聲明的pipe在頁面中引用有效,而在頁面中引用的component中的模板則無效,這也是令我困惑的地方...
尋求了一些解決方案,大多是要注意得在根模塊中聲明,于是我做了個嘗試,將組件也寫成一個功能模塊,并在組件功能模塊中申明pipe,結(jié)果很欣喜,組件中的pipe生效了。
具體操作方法:
只需新建組件功能模塊,并在改模塊中申明pipe,myComponent.module.ts
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { myComponent } from 'myComponent.ts';
import { HelloPipe } from "hello.pipe.ts";
@NgModule({
declarations: [
myComponent,
HelloPipe
],
imports: [
IonicPageModule.forChild(myComponent)
],
exports: [
myComponent
]
})
export class MyComponent {}
大功告成,組件的模板引用pipe得以生效.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Angular5 服務(wù)端渲染實(shí)戰(zhàn)
本篇文章主要介紹了詳解Angular5 服務(wù)端渲染實(shí)戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Angular4.x通過路由守衛(wèi)進(jìn)行路由重定向?qū)崿F(xiàn)根據(jù)條件跳轉(zhuǎn)到相應(yīng)的頁面(推薦)
這篇文章主要介紹了Angular4.x通過路由守衛(wèi)進(jìn)行路由重定向,實(shí)現(xiàn)根據(jù)條件跳轉(zhuǎn)到相應(yīng)的頁面,這個功能在網(wǎng)上商城項目上經(jīng)常會用到,下面小編給大家?guī)砹私鉀Q方法一起看看吧2018-05-05
AngularJS select設(shè)置默認(rèn)值的實(shí)現(xiàn)方法
這篇文章主要介紹了AngularJS select設(shè)置默認(rèn)值的實(shí)現(xiàn)方法的相關(guān)資料,這里提供實(shí)現(xiàn)方法幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
angular json對象push到數(shù)組中的方法
下面小編就為大家分享一篇angular json對象push到數(shù)組中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
AngularJS 打開新的標(biāo)簽頁實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了angularJS 打開新的標(biāo)簽頁方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09

