5

I need to display the array quality which is located inside an array of objects. I have tried calling it in ngFor using the code below. Is there something wrong with my usage of ngFor?

import { Component} from '@angular/core';

@Component({
    selector: 'my-app',
    template: `<table>
                  <thead>
                    <tr>
                       <th>Name</th>
                       <th>Quality1</th>
                       <th>Quality2</th>
                    </tr>
                  </thead>
                  <tbody>
                      <tr *ngFor"let item of people">
                         <td>{{item.Name}}</td>
                         <td *ngFor"let item of people.quality">item.quality1</td>
                         <td *ngFor"let item of people.quality">item.quality2/td>
                       </tr>
                  </tbody>
              </table>
   })

  export class AppComponent{    
     people: any = [{Name:"John", quality:[{quality1: "nice", quality2: "humble"}]}, {Name:"Dave", quality:[{quality1: "kind", quality2: "caring"}]} ];
  }

As of now only the names are appearing in the table but I would also like the quality to appear.

4
  • What are the second and third loops supposed to be doing? Will every person have two qualities? Why are the qualities in a map? Why use i as the name of each person in the loop? You're missing a closing backtick too. Commented Nov 23, 2016 at 8:55
  • @jonrsharpe the second and third loop are going to display the the quality of a person, I use "let item of people" and "let item of people quality" in my real code this is just a shortcut Commented Nov 23, 2016 at 9:03
  • @jonrsharpe is there a way to display my array using ngFor? Commented Nov 23, 2016 at 9:04
  • The second loop should be over i.quality, but your data model seems very weird and you're going to end up with an odd-looking table. Please show a minimal reproducible example of real code, rewriting it badly is not helpful. Commented Nov 23, 2016 at 9:06

3 Answers 3

2

We can simply iterate the loop inside by modifying the second loop

        <tr *ngFor="let item of people">
            <td>{{item.Name}}</td>
            <td *ngFor="let quality of item.quality">{{ quality }}</td>
            <td *ngFor="let quality2 of item.quality2">{{quality2}}</td>
        </tr>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use <ng-container> to combine several nodes with *ngFor without introducing another element to the DOM:

<ng-container *ngFor"let item of people.quality">
  <td>item.quality1</td>
  <td>item.quality2/td>
</ng-container>

Comments

0

If I understand you well you could also bind like that:

                   <tr *ngFor="let item of people">
                     <td>{{item.Name}}</td>
                     <td *ngFor="let item of people.quality">{{item.quality.[quality1]}}</td>
                     <td *ngFor="let item of people.quality">{{item..quality.[quality2]}}</td>
                   </tr>

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.