自定義 Pipe
Pipe 會在 template 上針對資料作格式化
話不亦遲,來建個 pipe
ng g p pipes/change
這條指令會建立 src/pipes/change.pipe.ts
接著實作內容:
import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe, PercentPipe } from '@angular/common';
import { StockInterface } from '../services/stocks.service';
@Pipe({
name: 'change'
})
export class ChangePipe implements PipeTransform {
constructor(
private currencyPipe: CurrencyPipe,
private percentPipe: PercentPipe
) {}
transform(stock: StockInterface, showPercent: boolean = true): any {
let value = `${this.currencyPipe.transform(stock.change, 'USD', 'symbol', '.2')}`;
if(showPercent) {
value += `${this.percentPipe.transform(stock.changeInPercent, '.2')}`;
}
return value;
}
}
接著我們就可以在 SummaryCopmonent 內使用 ChangePipe
{{ stock | change }}
接著來建立一個 impure pipe
Impure 的原因不是因為不單純,是因為每一次即使給相同的 input,也不會得到相同的 output
再次提醒,每次 change detection 發生時,就會觸發 impure pipe 的作用
接著來建立一個叫 ChangeDetectorPipe 的 impure pipe
ng g p pipes/change-detector
並實作內容如下:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'changeDetector',
pure: false
})
export class ChangeDetectorPipe implements PipeTransform {
count: number = 0;
transform(value: any, args?: any): any {
this.count++;
console.log(`Component change detection executed ${this.count} times`);
return value;
}
}
再來調整 app.component.html
<span class="mdl-layout-title">{{'Stock Tracker' | changeDetector}}</span>
接著來建立一個會從後端 API 取值並回填到 view 上的 pipe
ng g p pipes/news
調整其實作如下:
import { Pipe, PipeTransform } from '@angular/core';
import { StocksService } from '../services/stocks.service';
@Pipe({
name: 'news',
pure: false
})
export class NewsPipe implements PipeTransform {
cachedSource: string = '';
news: string = 'loading...';
constructor(private service: StocksService) { }
transform(source: string, args?: any): any {
if (source !== this.cachedSource) {
this.cachedSource = source;
this.service.getNewsSnapshot(source).subscribe(news => {
this.news = `<a href="/${news.url}" target="_blank">${news.title}</a>`;
});
}
return this.news;
}
}
接著就是在 AppComponent 使用這個 pipe
<div class="mdl-layout-spacer"></div>
<span>Top News Headline: <span [innerHTML]="'cnbc' | news"></span></span>
小結: