[Angular] Create a custom pipe

For example we want to create a pipe, to tranform byte to Mb.

We using it in html like:

    <div>
      <div *ngFor="let file of files">
        <p>{{ file.size | filesize: 'MB' }}</p>
      </div>
    </div>

Create pipe:

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
  name: 'filesize'
})
export class FileSizePipe implements PipeTransform{
  transform(value: number, ext: string = "MB") {
    return (value / (1024 * 1024)).toFixed(2) + ' ' + ext;
  }
}
原文地址:https://www.cnblogs.com/Answer1215/p/6546119.html