angular2 add “Read More” link to custom pipe

℡╲_俬逩灬. 提交于 2020-01-15 08:52:09

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/40718823/angular2-add-read-more-link-to-custom-pipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!