【问题标题】:access value in array from Json response angular 4从 Json 响应角度 4 访问数组中的值
【发布时间】:2018-04-20 21:48:21
【问题描述】:

我想在表格的数组中显示一个字段..如何使用 angular 4 在我的 Json 响应中访问 SubjectCode? .. 我的代码给了我错误 [Error trying to diff '[object Object]'。只允许使用数组和可迭代对象]

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')
                });

【问题讨论】:

  • 您的控制台日志显示什么?
  • 它显示 json 响应 @tricheriche
  • 您在对象而非数组上使用 *ngFor。你应该在你的 *ngFor 上使用subjectsWithID.subjects
  • 是的,感谢您的帮助

标签: json angular http typescript


【解决方案1】:

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

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

【讨论】:

  • 好吧,因为您在对象上使用 ngFor,而不是数组。
  • 太好了,记得将@Sajeetharan 的答案标记为已解决!
【解决方案2】:

你应该在主题上使用 ngFor,代码应该是,

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

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2020-12-11
    相关资源
    最近更新 更多