2017-08-10 80 views
1

我需要迭代锚标记中对象数组的对象。以下是我的代码的格式对象数组的对象的迭代Angular4

{"1":{"uri":"test1","name":"test1","icon":"http:\/\/dummyurl\/images\/icons\/test1-icon.png","application_id":1},"2":{"uri":"test2","name":"test2","icon":"http:\/\/dummyurl\/images\/icons\/test2-icon.png","application_id":2},"3":{"uri":"test3","name":"test3","icon":"http:\/\/dummyurl\/images\/icons\/test3-icon.png","application_id":3},"4":{"uri":"test4","name":"test4","icon":"http:\/\/dummyurl\/images\/icons\/test4-icon.gif","application_id":4},"5":{"uri":"test5","name":"test5","icon":"http:\/\/dummyurl\/images\/icons\/test5-icon.png","application_id":5}}

的.ts文件

this.applicationService.getApplication(id) 
    .subscribe(applications => { 
    this.applications = applications.response;}); 

HTML

<a class="dropdown-item" href="#" NgForOf=" let obj of applications"> 
    {{obj.name}} </a> 

我被困在这个从去年3-4小时。任何帮助将不胜感激。

+2

采取格式化你的代码,并在发布前正确提问的时间。此外,你还没有解释什么不适用于你发布的代码或你做了什么来解决它,或者你确切的困惑是什么。 – csmckelvey

+0

钥匙是连续的数字? – alehn96

回答

1

您可以通过他们循环使用Object.keys(apps)

this.applicationService.getApplication(id) 
    .map(result => result.response) 
    .subscribe(apps=> { 
    this.applications = Object.keys(apps).map(k => apps[k]) 
    }); 

HTML

<div *ngFor="let app of applications">{{app | json}}</div> 
+0

非常感谢@KId。你不仅节省了我的时间。你也救了我:)我正在寻找这个快速和简短的答案。 –