2017-02-13 7 views
0

[追加]ボタンをクリックすると、新しい行が作成されます。私は3行を持っていると私は削除ボタンを使用して2番目の行の値を削除する。anglejsを使用して特定の行の値を削除する方法

[削除]ボタンをクリックすると、行は削除されますが、最後の行インデックスの値はデフォルトで削除されます。

私はフィドル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

ng-click="removelanglist($index)"にあなたの期待どおりに動作します私の提案されたソリューションを使用してみてください、リンクjsfiddleでデモを確認してください。 –

答えて

0

を作成しているあなたのための項目を削除し、そのthis updated fiddleをご覧ください。

更新された削除関数は、テンプレートから渡された項目を使用し、配列内のその項目のインデックスを検索します。後述するように、そのインデックスは、その後のスプライス方法で使用されています

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

ありがとうございます。これはちょうど私が欲しいです – sudarsan

0

Swicth:

ng-click="removelanglist(index)"

+0

はい私は変更しましたが、私は同じように動作します。 2行目の削除ボタンをクリックすると、最後の行が削除されます。 – sudarsan

+0

私には同じことは起こりません。あなたの[フィドル]でこれを試してみた(http://jsfiddle.net/6Texj/290/) – Korte

関連する問題