0

I am new to angular 9.

I need to convert my data (I get them from api) to array and then to display last 5 records with keys and values to material modaldialog. I have seen this and some other suggestions, but it didn't work for me. What is the best approach to do that?

This is what I have try so far:

parent component:

openDialogNewNotify(): void {


    this.httpClient.get<Ticket[]>(this.API_URL + '/AllIssues').subscribe(data => {
    this.dataChange.next(data);
     //const objectArray = Object.entries(data);


    this.dialog.open(NotifyNewComponent, {
      width: '320px',
      height: '200px',

      data: {
        Issue: // What I need to put here? 


      },

    });
  }

html:

<div *ngFor="let item of data | keyvalue">
{{item.key}}:{{item.value}}
</div>
touinta
  • 971
  • 3
  • 10
  • 26

1 Answers1

0

Use slice pipe provided by angular. Somethng like this

<li *ngFor="let num of myArray | slice:myArray.length:-1">{{num}}</li>

In your case

<div *ngFor="let item of data | slice:myArray.length:-1 | keyvalue">
 {{item.key}}:{{item.value}}
</div>

The advantage of this approach is that you are doing the slice only in the template level. In ts you will get all data that helps you in any other operations with rest of the elements.

Arun Mohan
  • 1,207
  • 1
  • 5
  • 11