我已经创建了客户摘要管道类,它工作正常,但我想在子字符串的末尾添加阅读更多链接.. 单击显示的所有内容时。
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; ?
}
}
Run Code Online (Sandbox Code Playgroud)
我需要填写我知道的 fucn 但我需要问什么是更有效和更真实的方法来实现这一点?
最佳实践可能是将管道逻辑与“阅读更多”按钮分开。
另外,我建议您使用模块中的shorten
管道ng-pipes
:https : //github.com/danrevah/ng-pipes#shorten
用法示例:
在控制器中:
this.longStr = 'Some long string here';
this.maxLength = 3
this.showAll = () => this.maxLength = this.longStr.length;
Run Code Online (Sandbox Code Playgroud)
在视图中:
<p>{{longStr | shorten: maxLength: '...'}}</p>
<div *ngIf="longStr.length > maxLength" (click)="showAll()">Read More</div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3347 次 |
最近记录: |