1

I am new in Angular and I am doing a web service request to populate a table. I am using the *ngFor to populate my table. I receive my email correctly but the other elements in tree show as [Object object]

This is my JSON response:

{
    "data": [  
        {
            "id": 1,
            "email": "[email protected]",
            "password": "$2a$10$44ghfG4Ym4COxXbj9pDBuOLBXCPRRDiIM7y77G.XEh7avm2GOxlUC",
            "isAdmin": 0,
            "acessTypes": [
                {
                    "id": 1,
                    "accessTypeName": "User",
                    "subAcessTypeName": "Ver&Escrever"
                }
            ],
            "tomas": [],
            "consultas": [],
            "profile": "NORMALUSER"
        }
    ],
    "dataArray": null,
    "errors": []
}

This is my Angular Code´

<table class="table table-bordered">
          <tr>
            <th>Email</th>
            <th>Tipo de acesso</th>
            <th>SubTipo de acesso</th>
          </tr>
          <tr *ngFor="let user of listUser">
            <td>{{user.email}}</td>
            <td>{{user.acessTypes}}</td>
            <td>{{user.acessTypes}}</td>
          </tr>
        </table>

This is my User component

findAll(){
    this.userService.findAll().subscribe((responseApi: ResponseApi)=>{
      this.listUser = responseApi['data'];
    }, err => {
      this.showMessage({
        type: 'error',
        text: err['error']['error'][0]
      });
     }
    )
  }
4
  • 2
    If you add json pipe, that should show you full data <td>{{user.acessTypes | json}}</td>. But you definitely need another loop over there to access individual object properties Commented Sep 25, 2018 at 14:26
  • if you simply want to see the all the elements in the array, just use the json pipe like <td>{{ user.acessTypes | json }}</td> Commented Sep 25, 2018 at 14:26
  • Can you show an example? Commented Sep 25, 2018 at 14:26
  • I just want to see the name and the accessType Commented Sep 25, 2018 at 14:27

3 Answers 3

2

If you add json pipe, that should show you full data <td>{{user.acessTypes | json}}</td>

But you have to loop through acessTypes array to access individual object properties

<table class="table table-bordered">
    <tr>
        <th>Email</th>
        <th>Tipo de acesso</th>
        <th>SubTipo de acesso</th>
    </tr>
    <tr *ngFor="let user of listUser">
        <td>{{user.email}}</td>
    <ng-container *ngFor="let type of user.acessTypes">
        <td>{{type.accessTypeName}}</td>
         <td>{{type.subAcessTypeName}}</td>
    </ng-container>
    </tr>
</table>

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, very helpfull :)
1

Obviously, because user.acessTypes is an array of object.

You need to access specific values from object.

<tr *ngFor="let user of listUser">
            <td>{{user.email}}</td>
            <td *ngFor="let i of user.acessTypes">{{i.accessTypeName}}</td>
            <td *ngFor="let i of user.acessTypes">{{i.subAcessTypeName}}</td>
</tr>

or using one inner loop.

    <tr *ngFor="let user of listUser">
                <td>{{user.email}}</td>
               <ng-container *ngFor="let i of user.acessTypes" >
                <td>{{i.accessTypeName}}</td>
                <td>{{i.subAcessTypeName}}</td>
               </ng-container>
    </tr>

8 Comments

Nope, because it return an empty string :)
an object will contain empty string as well. :)
The problem is that the object(as you can see in the JSON request) does not contain an empty string
that's an array so you need to use indexing over the accessTypes.
Thanks thats it :)
|
0

user.acessTypes is an array.

Use index as below.

<td>{{user.acessTypes[0].accessTypeName}}</td>
<td>{{user.acessTypes[0].subAcessTypeName}}</td>

3 Comments

But the user can have more than one accessType :)
As in one of the comments, then you would need another ngFor
Can you show an example on how to do a nested ngFor?

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.