【问题标题】:angular2 add "Read More" link to custom pipeangular2 将“阅读更多”链接添加到自定义管道
【发布时间】:2017-04-04 18:26:45
【问题描述】:

我已经创建了自定义摘要管道类,它工作正常,但我想在子字符串的末尾添加一个阅读更多链接。当点击所有显示的内容时。

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; ?
    }
}

我需要填写我知道的fucn,但我需要问什么是更有效和更真实的方式来完成这个?

【问题讨论】:

    标签: angular2-pipe


    【解决方案1】:

    最佳实践可能是将管道逻辑与“阅读更多”按钮分开。

    另外,我建议你使用来自ng-pipes 模块的shorten 管道:https://github.com/danrevah/ng-pipes#shorten

    使用示例:

    在控制器中:

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

    在视图中:

    <p>{{longStr | shorten: maxLength: '...'}}</p>
    <div *ngIf="longStr.length > maxLength" (click)="showAll()">Read More</div>
    

    【讨论】:

    • 我试过这个,它适用于单个帖子,但它不适用于单个页面上的多个帖子。我点击阅读更多,所有其他帖子也受到影响。我如何才能仅在我选择阅读更多的单个帖子中实现这一点
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2017-03-20
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2021-04-21
    相关资源
    最近更新 更多