2016-07-28 16 views
2

私はここに少しジレンマがあります。私は(jQueryのUIソート可能に基づく)NGリピート内にあるとAngularJS UI Sortableを使用してソート可能であるネストされたJSONオブジェクトを持っている: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を使ってそれを削除しています(配列の場合)。

しかし、このように動作するように見えるdoesntのオブジェクトのために、私は私のボタンで...

を代わりに使用してください削除オンラインを読んで、私はのようにオブジェクト自体を渡ししよう:

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()関数でリストとインデックスの両方を受け取るようにして、インデックス付きの要素を削除して、あなたのビューを更新します。

+0

これは質問には答えません。それはすでに別の方法で問題になっているものを再提示しています。 – charlietfl

+0

質問は文字通り「任意の提案ですか?私は彼に、詳細と正当な理由で、彼がやろうとしていたことを続けていくよう提案しました。 –

関連する問題