Search code examples
angularjsarraysjsoncontrollerangular-ngmodel

How to assign multi level Json data array to single Angular object?


I have a json data like below

{
  "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
      }
    }]
  }
}

In this Json data there is 'activeGoal' array,and inside of that there is an another array 'examDetails'

In Controller,I tried assigning these data into the angular object like below

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

I am getting activeGoals, but for examDetails I am getting 'undefined' error. What is the wrong i am doing here??


Solution

  • assign like this : examCtrl.examDetails=data.activeGoals[0].examDetails;

    since activeGoals is itself an array and you need first index([0]) value of it i.e., examDetails.

    Further: as per your JSON examDetails is object not array.