Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieve Data in Angular from JSON object

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading