Home > Blockchain >  Retrieve Data in Angular from JSON object
Retrieve Data in Angular from JSON object

Time:12-15

How do I retrieve only the first word , in this case "Koobiyo" from angular html?

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

I want to access from it here, like this?

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

how do I do that?

CodePudding user response:

You can create a simple pipe,

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(" ");
  }
}

and use it as,

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

Stackblitz Demo

  • Related