AngularJS从Json对象中删除一个子对象 [英] Angularjs Remove a Child Object from Json Object
本文介绍了AngularJS从Json对象中删除一个子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试删除Child对象时,出现错误
When i try to remove Child object i am getting an error
TypeError:ctrl.otsact.tests.splice不是函数
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对象中删除子对象的
Script : i am suing splice to remove the child object form json object
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
}
]
}
}
}
}
我在上面的脚本中做错了什么
What i am doing wrong in the above script
谢谢.
推荐答案
这是一个对象,而不是数组,因此无法对其进行拼接. 更改此:
This is an object not an array, so you can't splice it. Change this:
ctrl.otsact.tests.splice(index, 1);
对此:
delete ctrl.otsact.tests[index];
这篇关于AngularJS从Json对象中删除一个子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文