Skip to content

Instantly share code, notes, and snippets.

@ygamretuta
Last active March 7, 2017 10:33
Show Gist options
  • Save ygamretuta/c8cc6dfbb2bb5352aae6219c36eb1cfe to your computer and use it in GitHub Desktop.
Save ygamretuta/c8cc6dfbb2bb5352aae6219c36eb1cfe to your computer and use it in GitHub Desktop.
custom pipe that truncates a long string to a certain number of chars; uses lodash
// pipes/truncate.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
import * as _ from 'lodash';
@Pipe({ name: 'truncate' })
export class TruncatePipe implements PipeTransform {
transform(value: string, chars?: number): string {
let length = chars ? chars : 100;
return _.truncate(value, {length: length });
}
}
// app.module.ts
import { TruncatePipe } from './pipes/truncate.pipe';
@NgModule({
declarations: [TruncatePipe]
})
// my.component.html
<span>{{myObject.description | truncate:50 }}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment