【问题标题】:Angularjs delete JSon objectAngularjs 删除 JSON 对象
【发布时间】: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


【解决方案1】:

使用ngModelItem

<li ng-repeat="innerItem in ngModelItem.items">
    <a href="#" ng-click="deleteItem(ngModelItem.items, $index)">Delete me</a>

在您的控制器中,

$scope.deleteItem = function(collection, index){
    collection.splice(index,1);
};

Demo

【讨论】:

    【解决方案2】:

    要从 json 对象中删除 json 元素,请使用 delete operator。但在你的情况下,我假设你想从 json 数组中删除一个 json 对象,所以你真的应该使用 splice() 来代替。

    您应该在 removeItem() 函数中同时收到列表和索引,以便您可以删除索引元素,并且 angularjs 将更新您的视图。

    【讨论】:

    • 这并没有回答问题,它只是以不同的方式重申问题中已经存在的内容
    • 问题的字面意思是“有什么建议吗?”。我给了他一个建议,让他继续他打算做的事情,并提供更多细节和理由。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2018-07-30
    • 1970-01-01
    • 2013-12-17
    • 2017-03-25
    相关资源
    最近更新 更多