2013-07-30 103 views
1

我有下面的代碼從AngularJS列表中刪除項目:鏈接按鈕動作

$scope.removeItem = function() { 
    if($scope.items.indexOf(toDelete) > -1) { 
    var index = $scope.items.indexOf(toDelete); 
    $scope.items.splice(index, 1); 
    } 
}; 

而下面的代碼在我玉模板:

div.row.spacing-small(ng-repeat='item in items') 
    div.col-lg-4: p {{item}} 
    div.col-lg-2: button.btn.btn-danger.btn-block Delete 

的項目對象是基本上是一個數組,如下所示:['foo','bar']。有沒有辦法連接「刪除」按鈕與removeItem函數?我仍然在玩這個夢幻般的框架,但它全是如此新穎,有時很難在文檔中找到正確的東西。

回答

2
div.row.spacing-small(ng-repeat='item in items') 
    div.col-lg-4: p {{item}} 
    div.col-lg-2: button.btn.btn-danger.btn-block(ng-click=\"removeItem($index)\") 


$scope.removeItem = function(index) { 
    $scope.items.splice(index, 1); 
}; 

這應該有助於

修訂

JSFIDDLE

0

我會使用ng-click指令並調用removeItem(item)。由於您正在執行ng-repeat,因此每個重複的塊都將具有正確的項目引用。另外,我發現在從ng-repeat調用函數時,最簡單的方法是讓函數參考它應該修改的對象。