2016-07-28 137 views
2

所以我在这里几乎没有困难。我有一个嵌套的JSON对象,它是NG重复内,可排序使用AngularJS UI Sortable(基于jQuery UI可排序):Angularjs删除JSon对象

$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) { 

    }; 

有什么建议吗?

+0

你会知道,鉴于无论是父对象或子阵列项目不好吗?需要了解视图的结构 – charlietfl

回答

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

0

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

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

+0

这并不回答这个问题,它只是以不同的方式重述问题中已有的内容 – charlietfl

+0

问题实际上是“任何建议?”。我给了他一个建议,继续他打算做的事情,提供更多的细节和理由。 –