簡體   English   中英

AngularJS將子對象添加到Json對象

[英]Angularjs add a Child Object to Json Object

當我嘗試將Children添加到Json Object時出現問題:

TypeError:ctrl.otsact.tests.push不是函數

劇本:

function addOTSACT() {
        var data = {
            'date': '',
            'subjects': angular.copy(ctrl.otsactSubjectsCopy)
        }
        $('.dateField').datepicker({
        changeMonth: true,
        changeYear: true
        });
        ctrl.otsact.tests.push({});
    }

傑森對象:

{  
   "ACT":{  
      "name":"ACT",
      "tests":{  
         "73":{  
            "id":73,
            "official_test_id":1,
            "student_id":165888,
            "test_date":"2017-05-12",
            "score":"0.0",
            "created_at":"2017-05-23 13:50:40",
            "created_by_id":2766,
            "updated_at":"2017-05-23 13:50:40",
            "updated_by_id":2766,
            "subjects":[  
               {  
                  "id":1,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":1,
                  "score":1,
                  "student_score_id":73,
                  "name":"English",
                  "is_consider":1
               },
               {  
                  "id":2,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":2,
                  "score":1,
                  "student_score_id":73,
                  "name":"Math",
                  "is_consider":1
               },
               {  
                  "id":3,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":3,
                  "score":1,
                  "student_score_id":73,
                  "name":"Reading",
                  "is_consider":1
               },
               {  
                  "id":4,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":4,
                  "score":1,
                  "student_score_id":73,
                  "name":"Science",
                  "is_consider":1
               },
               {  
                  "id":5,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":5,
                  "score":1,
                  "student_score_id":73,
                  "name":"Writing",
                  "is_consider":0
               }
            ]
         },
         "74":{  
            "id":74,
            "official_test_id":1,
            "student_id":165888,
            "test_date":"2017-05-12",
            "score":"0.0",
            "created_at":"2017-05-23 13:50:40",
            "created_by_id":2766,
            "updated_at":"2017-05-23 13:50:40",
            "updated_by_id":2766,
            "subjects":[  
               {  
                  "id":1,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":1,
                  "score":2,
                  "student_score_id":74,
                  "name":"English",
                  "is_consider":1
               },
               {  
                  "id":2,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":2,
                  "score":2,
                  "student_score_id":74,
                  "name":"Math",
                  "is_consider":1
               },
               {  
                  "id":3,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":3,
                  "score":2,
                  "student_score_id":74,
                  "name":"Reading",
                  "is_consider":1
               },
               {  
                  "id":4,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":4,
                  "score":2,
                  "student_score_id":74,
                  "name":"Science",
                  "is_consider":1
               },
               {  
                  "id":5,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":5,
                  "score":2,
                  "student_score_id":74,
                  "name":"Writing",
                  "is_consider":0
               }
            ]
         }
      }
   }
}

我想增加75個孩子來測試孩子

您的tests屬性是...一個object ,而不是一個array 測試應該是tests: [] ,而不是tests: {}

在您的JSON測試中,不是數組而是對象,因此您不能推送到對象。 所以這行是錯誤的:

ctrl.otsact.tests.push({});

您必須將其更改為:

ctrl.otsact.tests[MYNEWID] = MYNEWOBJECT;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM