最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
當前位置: 首頁 - 科技 - 知識百科 - 正文

Angular2的管道Pipe的使用方法

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 22:25:53
文檔

Angular2的管道Pipe的使用方法

Angular2的管道Pipe的使用方法:管道Pipe可以將數(shù)據(jù)作為輸入,然后按照規(guī)則將其轉(zhuǎn)換并輸出。在Angular2中有許多內(nèi)置的Pipe,比如DatePipe、UpperCasePipe和CurrencyPipe等。在這里我們主要介紹如何自定義Pipe。 1. 管道定義 Pipe的定義如下代碼所示: import { PipeTr
推薦度:
導讀Angular2的管道Pipe的使用方法:管道Pipe可以將數(shù)據(jù)作為輸入,然后按照規(guī)則將其轉(zhuǎn)換并輸出。在Angular2中有許多內(nèi)置的Pipe,比如DatePipe、UpperCasePipe和CurrencyPipe等。在這里我們主要介紹如何自定義Pipe。 1. 管道定義 Pipe的定義如下代碼所示: import { PipeTr

管道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);
 }
}

如代碼所示,

  1. 需要使用@Pipe來裝飾類
  2. 實現(xiàn)PipeTransform的transform方法,該方法接受一個輸入值和一些可選參數(shù)
  3. 在@Pipe裝飾器中指定管道的名字,這個名字就可以在模板中使用。

注意:當定義完成后,不要忘記在Module的declarations數(shù)組中包含這個管道。

2. 管道使用

user.template.html實現(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實現(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。

當啟動應用時,可以發(fā)現(xiàn)只有id大于3的user被顯示出來了??墒牵斈泓c擊按鈕添加用戶時,發(fā)現(xiàn)并沒有什么反應,數(shù)據(jù)并沒有改變。這是為什么呢?

3. 數(shù)據(jù)變更檢測

在Angular2中管道分為兩種:純管道和非純管道。默認情況下管道都是純管道。

純管道就是在Angular檢測到它的輸入值發(fā)生了純變更時才會執(zhí)行管道。純變更也就是說原始數(shù)據(jù)類型(如String、Number和Boolean等)或者對象的引用發(fā)生變化。該管道會忽略對象內(nèi)部的變化,在示例中,數(shù)組的引用沒有發(fā)生改變,改變的只是數(shù)組內(nèi)部的數(shù)據(jù),所以當我們添加數(shù)據(jù)時并沒有立即響應在頁面上。

非純管道會在組件的變更檢測周期中執(zhí)行,而且會對對象的內(nèi)部數(shù)據(jù)進行檢測。

在我們的示例中將管道變更為非純管道是非常賤簡單的,只要在管道元數(shù)據(jù)中將添加pure標志為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);
 }
}

這樣每當我們添加新用戶時,數(shù)據(jù)就會馬上響應在頁面上了。

在根模塊聲明的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得以生效.

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Angular2的管道Pipe的使用方法

Angular2的管道Pipe的使用方法:管道Pipe可以將數(shù)據(jù)作為輸入,然后按照規(guī)則將其轉(zhuǎn)換并輸出。在Angular2中有許多內(nèi)置的Pipe,比如DatePipe、UpperCasePipe和CurrencyPipe等。在這里我們主要介紹如何自定義Pipe。 1. 管道定義 Pipe的定義如下代碼所示: import { PipeTr
推薦度:
標簽: 使用 用法 pi
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top