2016-04-28 155 views
0

我有一個JSON對象是這樣的:AngularJS添加和嵌套JSON對象推

{ 
    "Name": "Shivansh", 
    "RollNo": "1", 
    "Stream": "CSE", 
    "OverallScore": "76", 
    "Semester": [ 
     { 
      "SemesterName": "FY-2012 - 1", 
      "TotalScore": "78.00", 
      "StartDate" : "2012-02-14", 
      "EndDate" : "2012-07-16", 
      "Amount" : "55000", 
      "Subject": [ 
       { 
        "subjectname": "maths", 
        "score": "81" 
       }, 
       { 
        "subjectname": "chemistry", 
        "score": "79" 
       }, 
       { 
        "subjectname": "physics", 
        "score": "77" 
       } 
      ] 
     }, 
     { 
      "SemesterName": "FY-2013-1", 
      "TotalScore": "76.00", 
      "StartDate" : "2013-02-16", 
      "EndDate" : "2014-07-16", 
      "Amount" : "55000", 
      "Subject": [ 
       { 
        "subjectname": "ADA", 
        "score": "80" 
       }, 
       { 
        "subjectname": "Operating System", 
        "score": "77" 
       }, 
       { 
        "subjectname": "Databases", 
        "score": "73" 
       }, 
       { 
        "subjectname": "Economics", 
        "score": "71" 
       } 
      ] 
     }     
    ] 
} 

現在我想用angularJS另一個學期字段添加到這個JSON。任何人都可以幫助我如何實現這一目標。我想補充的下一個字段可能看起來像:

$scope.addRowSubject = function() { 
      $scope.insertsub = { 
       //id = $scope.getSubject.length+1; 
       subjectname : '1', 
       score : '1' 
      }; 

      $scope.getSubject.push($scope.insertsub); 
}; 

getSubject是科目是存在於一個學期名單:

{ 
        "SemesterName": "FY-2013-2", 
        "TotalScore": "75.00", 
        "StartDate" : "2011-02-16", 
        "EndDate" : "2012-07-16", 
        "Amount" : "55067800", 
        "Subject": [ 
         { 
          "subjectname": "Sets and Probability", 
          "score": "78" 
         }, 
         { 
          "subjectname": "Networking and Security", 
          "score": "76" 
         }, 
         { 
          "subjectname": "Advanced DataBases", 
          "score": "72" 
         }, 
         { 
          "subjectname": "Economics-2", 
          "score": "70" 
         } 
        ] 
       } 

到目前爲止我使用這種類型的控制器下面所提到領域。我能夠毫無問題地得到這份清單。

$scope.addRowSemester = function() { 
      $scope.insertsem = { 
       //id = $scope.getSemester.length+1; 
       StartDate : "1", 
       EndDate : "1", 
       Amount : "1", 
       SemesterName : "1", 
       TotalScore : "1", 
       Subject : "" 
      } 
$scope.getSemester.push($scope.insertsem); 
}; 

getSemester是在學生的學期列表。

我能夠推送我的JSON內的一個學期字段,但作爲主題字段是null我不能推動主題領域。我希望你明白這個主意。 因此,任何關於此建議.. 在此先感謝。

+1

您可以加入一個plunkr? –

回答

1

您的JSONObject學科性質是應該是陣列的一個實例,像這樣:

$scope.addRowSemester = function() { 
      $scope.insertsem = { 
       //id = $scope.getSemester.length+1; 
       StartDate : "1", 
       EndDate : "1", 
       Amount : "1", 
       SemesterName : "1", 
       TotalScore : "1", 
       Subject : [] 
      }