[ Web Proxy ]
URL:
Viewing: https://angular.dev/api/core/PipeTransform [Back]  [Original]

PipeTransform Angular Skip to main content
menu
close
more_horiz
menuAPI
@angular/core

PipeTransform

interface
stable

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.

On this page

    arrow_upward_alt Back to the top

    API

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

    transform

    any
    @paramvalueany
    @paramargsany[]
    @returnsany

    Usage Notes

    In the following example, TruncatePipe returns the shortened value with an added ellipses.

    import {Pipe, PipeTransform} from '@angular/core';
    
    @Pipe({
      name: 'truncate',
    })
    export class TruncatePipe implements PipeTransform {
      transform(value: string) {
        return value.split(' ').slice(0, 2).join(' ') + '...';
      }
    }

    Invoking {{ 'It was the best of times' | truncate }} in a template will produce It was....

    In the following example, TruncatePipe takes parameters that sets the truncated length and the string to append with.

    import {Pipe, PipeTransform} from '@angular/core';
    
    @Pipe({
      name: 'truncate',
    })
    export class TruncatePipe implements PipeTransform {
      transform(value: string, length: number, symbol: string) {
        return value.split(' ').slice(0, length).join(' ') + symbol;
      }
    }

    Invoking {{ 'It was the best of times' | truncate:4:'....' }} in a template will produce It was the best.....

    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page