2016-07-28 133 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

問題實際上是「任何建議?」。我給了他一個建議,繼續他打算做的事情,提供更多的細節和理由。 –