I'm trying to create a read more pipe in angular, that renders custom HTML when the pipe is used. In the custom HTML can I access method in the component by passing it in the pipe?
readMorePipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'readMore'
})
export class ReadMorePipe implements PipeTransform {
transform(text: string, length: number = 20, showAll: boolean = false, suffix: string = '...'): any {
if (showAll) {
return text;
}
if (text.split(" ").length > length) {
text = text.split(" ").splice(0, length).join(" ") + suffix;
return `<button (click)="triggerReadMore()" style="color:red;"> //trying to pass function to the component and that function defined in that component
${text}
</button>`
}
return text;
}
}
Test.component.ts
<span innerHTML="{{someText | readMore:3:showAll}}"></span>
Test.component.ts
public showAll: boolean = false;
someText: string = "text text text text";
triggerReadMore(){
console.log("do something")
}
problems:
- rending HTML
button
- can we pass function name to the component somehow?(maybe use javascript)
- if there any better way please tell me