Skip to content

Instantly share code, notes, and snippets.

@mangar
Created October 5, 2016 18:22
Show Gist options
  • Select an option

  • Save mangar/aa55c72e5d36772098d94f44aa979287 to your computer and use it in GitHub Desktop.

Select an option

Save mangar/aa55c72e5d36772098d94f44aa979287 to your computer and use it in GitHub Desktop.
Truncate Pipe for Angular2
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'truncate'})
export class Truncate implements PipeTransform {
transform(value: string, args: string[]) : string {
let limit = args.length > 0 ? parseInt(args[0], 10) : 10;
let trail = args.length > 1 ? args[1] : '...';
return value.length > limit ? value.substring(0, limit) + trail : value;
}
}
@mangar
Copy link
Author

mangar commented Oct 5, 2016

Usage:
{{model.name | truncate : 15 : '.'}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment