I've created a custom pipe with the help of someone else's answer on stackoverflow. If article.title is larger than 55 characters then it should get trimmed after 55 characters and three dots should be there like a tail. For e.g:
- How to make a cake
- How to make cold c...
- Making curry at ho...
- Tasty Spanish eggs
Notice point 2 and 3 how extra characters are trimmed and replaced with 3 dots.
Here's the code:
ellipses.pipe.ts
import { Pipe } from '@angular/core';
import { SlicePipe } from '@angular/common';
@Pipe({
name: 'ellipsis'
})
export class EllipsisPipe extends SlicePipe {
constructor () {
super();
}
transform(value: string, maxLength: number): any {
const suffix = value && value.length > maxLength ? "..." : "";
return super.transform(value, 0, maxLength) + suffix;
}
}
Note: I tried ...implements PipeTransform also.
app.module.ts
@NgModule({
declarations: [
...,
EllipsisPipe // <-------------------- included in app module
],
imports: [
...
],
providers: [...],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<h5 class="card-title">{{article.title | ellipsis:55}}</h5>
Please correct my mistake. This question didn't help: Custom pipe returns an error. ::TypeScript & Angular2.
PS: Complete error is:
ERROR in src/app/pipes/ellipsis.pipe.ts(11,3): error TS2416: Property 'transform' in type 'EllipsisPipe' is not assignable to the same property in base type 'SlicePipe'. Type '(value: string, maxLength: number) => any' is not assignable to type '{ (value: readonly T[], start: number, end?: number): T[]; (value: string, start: number, end?: number): string; (value: null, start: number, end?: number): null; (value: undefined, start: number, end?: number): undefined; }'. Types of parameters 'value' and 'value' are incompatible. Type 'readonly any[]' is not assignable to type 'string'.
One strange thing is that It is working on stackblitz but giving the above error on localhost:4200.