2017-02-13 17 views
0

如果我單擊添加按鈕,則會創建新行。如果我有3行,我想使用刪除按鈕刪除第二行的值。如何使用angularjs刪除特定行的值

當我點擊刪除按鈕,然後行被刪除,但最後一行索引的值默認被刪除。

我創建了一個小提琴http://jsfiddle.net/6Texj/287/

<td > 
    <input type="text" name="count" class="form-control2" autocomplete="off" id="langlistNumber_{{$index}}"> 
</td> 
<td><input type="text" name="count" class="form-control3" autocomplete="off" id="langlist_{{$index}}"></td> 
<td><input type="text" name="count" class="form-control3" autocomplete="off" id="langurllist_{{$index}}"> 
    <a href ng-click="removelanglist(index)" ng-if="displayremovebutton">Remove</a> 
</td> 

$scope.addlanguagelist = function() { 
    debugger; 
    var index = $scope.languagelist.length; 

    console.log(index); 
    $scope.isCollapsed = true; 
    $scope.displayremovebutton = true; 
    $scope.languagelist.push({ 
     "model.urlListNumber": "", 
     "model.numbersUrlList": "", 
     "model.url": "", 
     "index": $scope.countBlocks 
    }); 

    $timeout(function() { 
     $scope.countBlocks++; 
    }, 200)}; 

    $scope.removelanglist = function(index) { 
      console.log(index); 
      $scope.languagelist.splice(index, 1); 
    }; 
+0

嘗試使用我所提出的解決方案,它的工作原理,按您的期望和檢查鏈接的jsfiddle演示。 –

回答

0

請看this updated fiddle爲你刪除的項目。

更新後的remove函數使用從模板傳遞的項目並查找該項目在數組中的索引。該索引然後用拼接方法使用如下所述:

$scope.removelanglist = function(item) { 
    var index = $scope.languagelist.indexOf(item); 
    $scope.languagelist.splice(index, 1); 
}; 
+0

謝謝。這正是我想要的 – sudarsan

0

Swicth:

ng-click="removelanglist(index)"ng-click="removelanglist($index)"

+0

是的,我改變了,但我的工作原理是一樣的。如果我點擊第二行的刪除按鈕,最後一行會被刪除。 – sudarsan

+0

同樣的事情不會發生在我身上。嘗試在你的[小提琴](http://jsfiddle.net/6Texj/290/) – Korte