html 从JSON对象检索Angular 数据

tzdcorbm  于 2022-12-16  发布在  Angular
关注(0)|答案(1)|浏览(120)

如何只检索第一个单词,在本例中是“Koobiyo”?
JSON对象:

{
    "otherInformation": "Koombiyo,Kurunegala,Kuliyapitiya"
}

我想从这里进入,像这样?
超文本:

<td>
    {{ issue.otherInformation }}
</td>

我该怎么做呢?

k10s72fa

k10s72fa1#

可以创建简单的管道,

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({ name: "sliceWords" })
export class SliceWordsPipe implements PipeTransform {
  transform(value: string, start: number, end?: number): string {
    if (value == null) return null;
    return value
      .split(",")
      .splice(start, end)
      .join(" ");
  }
}

并将其用作,

{{issue.otherInformation | sliceWords:0:1}}

Stackblitz Demo

相关问题