如何仅显示文本值的前15个字符[关闭]

问题描述 投票:0回答:2

来自数组[.ts文件]的文本显示在home.html

我只想显示15 characters,然后显示三个点...

这是我尝试使用的代码,但它不起作用

home.html

<ion-content>
  <!--<div class="info col-11"  [innerHtml]="list.dr_describe | safe: 'html'">{{list.q_describe}}</div>-->

  <div class="info col-11" *ngFor="let list of this.dailyreports">
    {{list.name}}
    <br/>
    <span [innerHtml]="list.desc | safe: 'html'">{{list.desc.substring(1,15)}}</span>
  </div>
</ion-content>

这是我的html页面的屏幕截图:

enter image description here

angular typescript ionic-framework ionic3 ionic4
2个回答
3
投票

尝试一下

  {{ (list.desc.length>15)? (list.desc  | slice:0:15)+'...':(list.desc ) }}

或使用自定义Pipe更好

@Pipe({
  name: 'shorten'
})
export class ShortenPipe implements PipeTransform {
  transform(val:string , length?: any):string[] {
    return (val.length>length)? val.slice(0, 15)+'...':val
  }
}

HTML中

{{list.desc | shorten:15 }}

2
投票

创建这样的自定义管道

@Pipe({
  name: 'customslice'
})
export class CustomSlice implements PipeTransform {
  transform(val:string , length:number):string {
    return val.length > length ? `${val.substring(0, length)} ...` : val
  }
}

并像这样使用它:

{{mystr | customslice : 15 }}
© www.soinside.com 2019 - 2024. All rights reserved.