Angular 如何自定义 pipe 管道以及参数传递问题
生活随笔
收集整理的這篇文章主要介紹了
Angular 如何自定义 pipe 管道以及参数传递问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下圖第11行代碼的 replace,是我自定義的 pipe 在Component 模板文件中的調用之處。標號1和2為其傳入的參數,通過冒號進行參數傳遞。
其中 wordStartPattern 為 replace pipe 的第一個參數,這是一個 Component 屬性:
第二個傳入 pipe 的參數為 $&,硬編碼。
而 pipe 接受的原始值,即 | 之前的值,這個值默認會始終傳入 pipe.
完整的實現代碼:
import { Pipe, PipeTransform } from '@angular/core';@Pipe({name: 'replace', }) export class ReplacePipe implements PipeTransform {transform(value: string,searchValue: string | RegExp,replaceValue: string,): string {const result = value.replace(searchValue, replaceValue);console.log(`Jerry own pipe, original value: ${value},search value: ${searchValue}, replaceValue: ${replaceValue}, result: ${result}`);return result;} }更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的Angular 如何自定义 pipe 管道以及参数传递问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Angular 动态控制 aside 标
- 下一篇: Angular 如何使用 Injecti