【问题标题】:How to assign multi level Json data array to single Angular object?如何将多级 Json 数据数组分配给单个 Angular 对象?
【发布时间】:2017-05-17 10:35:57
【问题描述】:

我有一个如下所示的 json 数据

{
  "data": {
    "points": 390,
    "medal": "gold",
    "activeGoals": [{
      "examType": full,
      "dateApplied": null,
      "status": "Active-GoodStanding",
      "term": 2,
      "amountPaid": 2500,
      "pointEarned": null,
      "examDetails": {
        "totalAmountPaid": 4500,
        "examDate": 1459449000000,
        "endDate": 1554057000000,
        "totalMarks": 100,
        "nextExamDate": 1493577000000,
        "interestRate": 0
      }
    }]
  }
}

在这个Json数据中有一个'activeGoal'数组,里面还有一个数组'examDetails'

在控制器中,我尝试将这些数据分配到如下的角度对象中

  examCtrl.details ={};
  examCtrl.details =data;
  examCtrl.activeGoals=data.activeGoals;
  examCtrl.examDetails=data.activeGoals.examDetails;

我得到了 activeGoals,但对于examDetails,我得到了“未定义”错误。 我在这里做错了什么??

【问题讨论】:

  • activeGoals 是一个数组。您应该像 data.activeGoals[1].examDetails 一样访问它

标签: angularjs arrays json controller angular-ngmodel


【解决方案1】:

这样分配:examCtrl.examDetails=data.activeGoals[0].examDetails;

因为activeGoals 本身就是一个数组,你需要它的第一个索引([0]) 值,即examDetails

进一步:根据您的 JSON examDetails 是对象而不是数组。

【讨论】:

    【解决方案2】:

    如果您对数据到底是什么感到困惑,您可以随时在 JSON 编辑器中进行转换。那么您可以轻松地从 JSON 数据中分配对象。

    【讨论】:

      猜你喜欢
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      相关资源
      最近更新 更多