[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;
  }
}

 

posted @ 2017-03-14 03:36  Zhentiw  阅读(149)  评论(0编辑  收藏  举报