【发布时间】:2016-07-28 16:24:36
【问题描述】:
所以我在这里有点两难。我有一个嵌套的 json 对象,它位于 ng-repeat 中,可以使用 AngularJS UI Sortable 进行排序(基于 JQuery UI Sortable):
$scope.rootItem = {
id: '1',
type: 'course',
title: 'Adobe Photoshop CC for beginners',
items: [{
id: '2',
type: 'label',
title:'label',
items:[{
id: '3',
type: 'module',
title:'Module title',
items: [{
id: '4',
type: 'topic',
title:'Topic title',
items: [{
id: '5',
type: 'content',
title:'Content title'
}, {
id: '6',
type: 'content',
title:'Content title'
}]
}]
},{
id: '7',
type: 'resources',
title:'Resources'
},{
id: '8',
type: 'module',
title:'Module title',
items: [{
id: '9',
type: 'topic',
title:'Topic',
items: [{
id: '10',
type: 'question',
title:'Question title'
}]
}, {
id: '11',
type: 'topic',
title:'Topic title',
items: [{
id: '12',
type: 'content',
title:'Content title'
}]
}]
}]
},{
id: '14',
type: 'assessmentLabel',
title: 'Assessment Label',
items: [{
id: '15',
type: 'assessment',
title: 'Assessment Title',
items: [{
id: '16',
type: 'courseAssessment',
title: 'Course Question Group',
items: []
}]
}]
}]
};
我应该能够做的是删除这个对象中的任何项目,如果它有任何孩子,他们也需要被删除。
所以我通常认为是传递 $index 并使用 splice 删除它(如果它是一个数组)。
但是对于对象似乎不是这样工作的,我在网上看到应该使用删除来代替......
在我的按钮上,我尝试传递对象本身,如下所示:
data-ng-click="removeItem(ngModelItem)"
在我的控制器中做这样的事情:
// Remove Item from the list
$scope.removeItem = function(item) {
};
有什么建议吗?
【问题讨论】:
-
您会在视图中知道它是父对象还是子数组项,不是吗?需要查看视图的结构
标签: javascript angularjs json angularjs-ng-repeat