AngularJS添加和嵌套JSON对象推

AngularJS添加和嵌套JSON对象推

问题描述:

我有一个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? –

您的JSONObject学科性质是应该是阵列的一个实例,像这样:

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