Angular2 - *ngFor / loop through json object with array

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

Want to loop through json object

//Defined on component side : jsonObj = {     '1' : [ {"title" : "title1" , "desc" : "desc1" }],     '2' : [ {"title" : "title2" , "desc" : "desc2" }],     '3' : [ {"title" : "title3" , "desc" : "desc3" }],     '4' : [ {"title" : "title4" , "desc" : "desc4" }],     '5' : [ {"title" : "title5" , "desc" : "desc5" }] } 

With *ngFor only on template side , And without any coding (function) on component side.

Want to print just each title and desc

Is this possible ? If Yes ? How ?

回答1:

Till now the best / shortesr answer I found is

Component side :

objectKeys = Object.keys; 

Template side :

Key: {{key}}
{{ obj.title }} {{ obj.desc }}

WORKING DEMO



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!