Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array of objects from angular template to another component using @input

I am trying to pass an array of objects through angular template from one component to another one.

 <div *ngFor="let item of data">
    <div class="col-xl-3">
      <top-users  usersData ={{[item]}}> </top-users>
    </div>
  </div>

item here supposed to be

[{id:1, name: 'Ahmed'}, {id:2, name:'Mohammed'}]

and the data is array of array of objects

[    
 [{id:1, name: 'Ahmed'}, {id:2, name:'Mohammed'}],
 [{id:3, name: 'Ahmed'}, {id:4, name:'Mohammed'}],
 [{id:5, name: 'Ahmed'}, {id:6, name:'Mohammed'}]
]

When I receive the variable through @input parameter, I get the result typeof string and looks like this

[object Object],[object Object]

I tried to parseJson and gives me this error

ERROR SyntaxError: Unexpected token o in JSON at position 1

What should I do to get the right array of object?

like image 519
palAlaa Avatar asked Sep 06 '25 17:09

palAlaa


1 Answers

You need to pass the data as below :

<div *ngFor="let item of data">
    <div class="col-xl-3">
      <top-users  [usersData]="item"> </top-users>
    </div>
  </div>

You will receive in child component as:

@Input() usersData;

usersData will look like:

[{id:1, name: 'Ahmed'}, {id:2, name:'Mohammed'}]
like image 146
Naranz Avatar answered Sep 11 '25 05:09

Naranz