如何以角度2显示嵌套对象和/或数组

时间:2016-07-12 07:44:24

标签: angularjs json angular ngfor

我从后端(api)获取json数据。我想用ngFor显示这个。我收到如下错误消息:“无法从控制台找到不同的支持对象'[object Object]'”

后来我试了这个:

@Pipe({
    name: 'mapToIterable'
})
export class MapToIterable implements PipeTransform{
    transform(map: {}, args: any[] = null): any {
        if (!map)
            return null;
        return Object.keys(map)
            .map((key) => ({ 'key': key, 'value': map[key] }));
    }
}

然后在我看来:

 <li *ngFor="let detail of getEventDetails | mapToIterable">
            Creator Email: {{detail.creator.email}}
     </li>

这次我没有收到错误,但没有{{detail.creator.email}}

的显示值

来自后端的数据

 {
        "banner_image": null, 
        "categories": [], 
        "creator": {
            "email": "email@email.com", 
            "first_name": "Prince", 
            "gender": true, 
            "id": 15, 
            "last_name": "Odame", 
            "subscribed_categories": [], 
            "watchlist": []
        }, 
        "creator_id": 15, 
        "description": "Learn how to install solar panels in 3 days and make real money all your lifetime", 
        "faqs": [], 
        "id": 6, 
        "is_verified": true, 
        "max_tickets_per_user": 1, 
        "shows": [
            {
                "address": "Engineering Auditorium, College of Engineering, KNUST, Kumasi", 
                "city": "Kumasi", 
                "country": "Ghana", 
                "end_time": "2016-08-03T14:30:00+00:00", 
                "event_id": 6, 
                "id": 5, 
                "is_online": false, 
                "start_time": "2016-08-01T08:30:00+00:00", 
                "state": "Ashanti", 
                "ticket_types": [], 
                "venue": "Engineering Auditorium"
            }
        ], 
        "tags": [], 
        "title": "Solar Panels Installation Workshop"
    }

提前致谢

1 个答案:

答案 0 :(得分:4)

您可能只想这样做:

<li>Creator Email: {{getEventDetails.creator.email}}</li>

对于阵列:

<li *ngFor="let show of getEventDetails?.shows">
   Show ID: {{show.id}}
</li>