i'm new to angular 2 , i used to get the excerpt of a text in angularjs with a filter like this :
app.filters.js:
app.filter('excerpt', function () {
return function (text, length) {
if (text.length > length) {
return text.substr(0, length) + '...';
}
return text;
}
});
in html file :
{{blog.content | excerpt:90}}
what's the equivalent in angular 2 ?
This should be doing it :
.ts
file :excerpt.filter.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'excerpt',
pure: false
})
export class ExcerptFilter implements PipeTransform {
transform(text: String, length:any ): any {
if (!text || !length) {
return text;
}
if (text.length > length) {
return text.substr(0, length) + '...';
}
return text;
}
}
template.component.html
<div class="description">{{blog.content | excerpt:90}} </div>
Make sure that you're importing your filter in app.module.ts
file :
import {ExcerptFilter} from './filters/excerpt.filter';
for more infos about creating pipes see the Docs