Last active
March 7, 2017 10:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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