0

I have this object:

    data = {
      campaign: {
        testeA: 1,
        testeB: 2,
        testeC: 3
      }
       category{
        categoryA: 15
       }
      ...
     }

I want to bind it on my template, so I tried using keyValuePipe

<p *ngFor= "let item of data| keyvalue"> {{item.key}}</p>

It's working and is showing the text campaign and category, but I need to show the results too How can I do it?

Desenv Junior
  • 101
  • 1
  • 1
  • 10

3 Answers3

0

you have to use the name of the variables to bind it to render it, like:

<p *ngFor= "let item of dados"> 
{{item.campaign.testeA}}
{{item.campaign.testeB}}
{{item.campaign.testeC}}
{{item.category.categoryA}}
</p>

Considering that dados is related to data and it's a list of this object

Raphael Marques
  • 699
  • 4
  • 16
0

You will probably need to add a second *ngFor statement to get the results e.g.:

<p *ngFor= "let item of data| keyvalue"> 
    <span *ngFor="let i of item | keyvalue">
      {{i.key}}
    </span>
</p>
Raffael
  • 249
  • 2
  • 7
0

It seems like you want to iterate over the keys and values of your nested objects too. You can accomplish this with the following code:

<div *ngFor="let categoryObject of data | keyvalue">
  <b>{{ categoryObject.key }}:</b>
  <div
    *ngFor="let innerCategoryObject of categoryObject.value | keyvalue"
    style="margin-left: 20px"
  >
    <b>{{ innerCategoryObject.key }}:</b>
    {{ innerCategoryObject.value }}
  </div>
</div>

There are a couple ways you can do this, including using the expanded version of *ngFor which will let you declare variables for the template. There's some tradeoff though as you can't type the variables you declare.

You can find other examples of how you can do this in this StackBlitz: https://stackblitz.com/edit/angular-r1nme6

Carlos Chaves
  • 51
  • 2
  • 4