admin管理员组文章数量:1290377
I am trying to pass an array of objects through angular template from one ponent 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?
I am trying to pass an array of objects through angular template from one ponent 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?
Share Improve this question asked Apr 11, 2019 at 7:52 palAlaapalAlaa 9,87833 gold badges109 silver badges170 bronze badges 1-
I dont think
[item]
is a right way. it should beitem
. tried to keep it simple this will reduce bugs. – Hoang Subin Commented Apr 11, 2019 at 8:03
2 Answers
Reset to default 7You 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 ponent as:
@Input() usersData;
usersData will look like:
[{id:1, name: 'Ahmed'}, {id:2, name:'Mohammed'}]
I had the same problem. I created the file with strict on, so I modified the strict to false in tsconfig.json
file.
"pilerOptions": {
"strict": false,
And also tried:
"pilerOptions": {
"noImplicitUseStrict": true,
If you don't find strict
just add it, don't forget to add ma(,
) after "strict": false,
. Next time create the file with -no--strict
. ex. ng new fileName -no--strict
版权声明:本文标题:javascript - Passing array of objects from angular template to another component using @input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741498695a2381954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论