Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access objects property name in angular template?

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?

like image 311
Arjun Avatar asked Nov 09 '25 02:11

Arjun


1 Answers

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' };
}
like image 110
devqon Avatar answered Nov 10 '25 16:11

devqon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!