2017-05-26 123 views
0

刪除的子對象。當我試圖刪除子對象我得到一個錯誤Angularjs從JSON對象

TypeError: ctrl.otsact.tests.splice is not a function

HTML:

<tr ng-repeat="act in ctrl.otsact.tests" ng-if="ctrl.editToggle"> 
    <td><span ng-click="ctrl.removeOTSACT(act.id)"> Delete </span></td> 
</tr> 

腳本: 我起訴剪接刪除的子對象形式json對象

function removeOTSACT(index) { 
    ctrl.otsact.tests.splice(index, 1); 
} 

Json對象

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

什麼,我在上面的腳本

感謝做錯了。

+0

則可能需要使用拼接,如果你的變量是一個對象之前JSON.parse你的變量? –

回答

1

這是一個不是數組的對象,所以你不能拼接它。 更改此:

ctrl.otsact.tests.splice(index, 1); 

這樣:

delete ctrl.otsact.tests[index]; 
0

在您的JSON表示中,ctrl.otsact.ACT.tests不是數組。如果您可以更改JSON,以便「測試」是一個數組,那麼拼接將起作用。