Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 add "Read More" link to custom pipe

I have created custome summary pipe class it works fine but I want to add a read more link end of substring.. when clicked all content shown.

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'summary' })
export class SummaryPipe implements PipeTransform {
    transform(value: string, maxWords: number) {
        if (value)
            return value.substring(0, maxWords) +"... <a href='#' (click)='getAllText()'>Read more</a>";
    }
       getAllText() {
        //return this.value; ?
    }
}

I need to fill fucn I know but I need to ask what is more efficient and true way of accomplish this?

like image 709
adeveloper Avatar asked Nov 20 '25 08:11

adeveloper


1 Answers

A best practice would probably be to separate the pipe logic from the 'read more' button.

Also, I would suggest you to use shorten pipe from ng-pipes module: https://github.com/danrevah/ng-pipes#shorten

Example of usage:

In the controller:

this.longStr = 'Some long string here';
this.maxLength = 3
this.showAll = () => this.maxLength = this.longStr.length;

In the view:

<p>{{longStr | shorten: maxLength: '...'}}</p>
<div *ngIf="longStr.length > maxLength" (click)="showAll()">Read More</div>
like image 169
D_R Avatar answered Nov 24 '25 04:11

D_R