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

Get string of nested object

I have an object that looks like this:

public serviceTable: Services[] = [
  { 1: { service: 'Service 1', description: 'Lorem Ipsum', serviceIsMandatory: true, serviceIsDisabled: true, price: 100, hourlyRate:100, hours: 5} },
  { 2: { service: 'Service 2', description: 'Lorem Ipsum', serviceIsMandatory: true, serviceIsDisabled: true, price: 100, hourlyRate:100, hours: 5} },
  { 3: { service: 'Service 3', description: 'Lorem Ipsum', serviceIsMandatory: true, serviceIsDisabled: true, price: 100, hourlyRate:100, hours: 5} }
];

Now in HTML, I want to output the values of service in my table.

<table mat-table [dataSource]="serviceTable" #matTableService class="mat-elevation-z8">
  <ng-container matColumnDef="service">
    <th mat-header-cell *matHeaderCellDef>Service</th>
    <td mat-cell *matCellDef="let element">{{ element[1].service }}</td>
  </ng-container>

Well, element[1].service works fine and outputs the string Service 1 but this is static and I want to make it dynamic.

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

I have also tried it with {{element.key | json }} but that outputs nothing. So how can I make it dynamic?

>Solution :

You can add let i = index; to iterate here.

<table mat-table [dataSource]="serviceTable" #matTableService class="mat-elevation-z8">
  <ng-container matColumnDef="service">
    <th mat-header-cell *matHeaderCellDef>Service</th>
    <td mat-cell *matCellDef="let element; let i = index;">{{ element[i].service }}</td>
  </ng-container>
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