从Json响应角度4访问数组中的值

时间:2017-11-08 09:20:45

标签: json angular http typescript

我想在表格中显示数组中的字段..如何使用角度4在我的Json响应中访问SubjectCode? ..我的代码给了我错误[错误尝试差异' [对象对象]'。只允许数组和迭代]

Json回复:

{
  "$id": "1",
  "Council_ID": 88,
  "place": "bla bla",
  "number": "45",
  "type": 1,
  "date": "2017-11-08T00:00:00",
  "time": "04:51",
  "user_Id": 15,
  "subjects": [
     {
        "$id": "2",
        "subjectCode": "45",
        "type": 1,
        "faculty": "medicine",
        "branch": "اسكندرية",
        "gender": true
     }
 ],
  "absence": []
}

meeting.component.html:

<table class="table table-hover table-condensed text-center">
                            <tbody>
                                <tr *ngFor="let subject of subjectsWithID">
                                    <td > {{subject.subjects.subjectCode}} </td>
                                </tr>
                            </tbody>
                        </table>

meeting.component.ts:

this.dataStorageService.getCouncilId(this.meetingID).subscribe(response => {
                    this.subjectsWithID = response.json();
                    console.log(this.subjectsWithID, 'all Json Resopnse Here')
                });

2 个答案:

答案 0 :(得分:2)

你应该使用ngFor over subject,代码应该是,

<tr *ngFor="let subject of subjectsWithID.subjects">
   <td > {{subject.subjectCode}} </td>
</tr>

答案 1 :(得分:1)

您的subjects属性是一个数组。如果你想显示它,那么你应该

{{subject.subjects[0].subjectCode}}