4

I want to bind a nested objects property name using angular interpolation

 <ng-container ngFor="let item of saleDetailsAggegater.productMap | keyvalue">
                            <tr *ngFor="let qtyMap of item.value | keyvalue">
                              <td>{{ item.key }}</td>
                              <td>{{qtyMap.value}}</td>
                              <td>{{Object.keys(qtyMap)[0]}}</td>
                            </tr>
                        </ng-container>

my saledetailsAggegater object is as follows

{"productMap":{"55478":{"priceQuantityMap":{"200.00":10}}},"taxMap":{},"paymentMap":{"1":970},"total":970}

but this line is not working as expected

<td>{{Object.keys(qtyMap)[0]}}</td>

how can i achieve this?

3
  • What is not working as expected? What is the expected outcome? I don't think you can use native javascript functions (like Object.keys) in your template Commented Oct 9, 2020 at 6:56
  • stackoverflow.com/questions/35534959/… Commented Oct 9, 2020 at 6:57
  • What do you want to achieve by Object.keys(qtyMap)[0]? Commented Oct 9, 2020 at 6:59

2 Answers 2

4

I don't think you can use native javascript functions in your template (angular won't parse them). You can however define the function on your component, and then use that in your template:

@Component({
  template: '<td>{{objectKeys(myObj)[0]}}</td>'
})
export class MyComponent {
  objectKeys = Object.keys;

  myObj = { displayThisKey: 'some-value' };
}
Sign up to request clarification or add additional context in comments.

Comments

1

first you have a typo in your first ngFor, you forgot to put the *.
and you can achieve what you want by doing the following:

<ng-container *ngFor="let item of saleDetailsAggegater.productMap | keyvalue">
    <tr *ngFor="let qtyMap of item.value | keyvalue">
        <td>{{ item.key }}</td>
        <td>{{qtyMap.value | json}}</td>
        <td>{{qtyMap.key | json}}</td>
        <td>{{(qtyMap.value | keyvalue | json)}}</td>
    <td *ngFor="let qty of (qtyMap.value | keyvalue);">
      {{qty.key}}
    </td>
    </tr>
</ng-container>

I deleted {{Object.keys(qtyMap)[0]}} and added a json pipe just to make sure the object has the values you wanted. I also added multiple possibilities for you to choose from.
and here is a live demo: link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.