填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

PipeTransform

一個需要由管道實現的介面,用於執行轉換操作。 Angular 會呼叫它的 transform 方法,並把要繫結的值作為第一個引數傳入,其它引數會依次從第二個引數的位置開始傳入。

An interface that is implemented by pipes in order to perform a transformation. Angular invokes the transform method with the value of a binding as the first argument, and any parameters as the second argument in list form.

      
      interface PipeTransform {
  transform(value: any, ...args: any[]): any
}
    

方法

transform(value: any, ...args: any[]): any
      
      transform(value: any, ...args: any[]): any
    
引數
value any
args any[]
返回值

any

使用說明

在下面的例子中,RepeatPipe 會把指定的值(value)重複指定的次數(times):

In the following example, RepeatPipe repeats a given value a given number of times.

import {Pipe, PipeTransform} from '@angular/core'; @Pipe({name: 'repeat'}) export class RepeatPipe implements PipeTransform { transform(value: any, times: number) { return value.repeat(times); } }
      
      import {Pipe, PipeTransform} from '@angular/core';

@Pipe({name: 'repeat'})
export class RepeatPipe implements PipeTransform {
  transform(value: any, times: number) {
    return value.repeat(times);
  }
}
    

在範本中呼叫 {{ 'ok' | repeat:3 }} 的結果是 okokok

Invoking {{ 'ok' | repeat:3 }} in a template produces okokok.