使用angularjs中的splice从数组中删除选定的行

时间:2017-08-15 17:09:42

标签: javascript angularjs

我在从数组中删除特定项目时遇到问题。我尝试过使用拼接,但最后一行是删除而不是特定的行。我在这里提供了plunker链接:

   $scope.rows.splice($index, 1); 

https://plnkr.co/edit/WETSLqOXlTwiHq4p9IUt?p=preview 任何帮助,将不胜感激 。感谢

4 个答案:

答案 0 :(得分:1)

just try the following 

http://jsfiddle.net/oymo9g2f/2/

you have some problem with your array splice 

答案 1 :(得分:0)

只需使用此代码即可。对于使用DataTable _datatable = new DataTable(); MySQLDataAdapter _adapter = new MySQLDataAdapter("SELECT * FROM TEST_TABLE", connection) _adapter.Fill(_datatable); myTextBox.Text = _datatable.Rows[0]["ID"].ToString(); 的文本区域以及控制器中的相关更改

在html中:刚刚显示ng-model="row.value"部分

ng-repeat

并在控制器中:

<div class="row" ng-repeat="row in rows track by $index">             
     <div class="col s12 m4" >
        <label for="destination_features1" >Features</label>
        <textarea  id="destination_features1" name="destination_features1_{{$index}}" ng-model="row.value" placeholder="Data Here"  type="text" ></textarea>                        
     </div>
     <button ng-show="show_removebtn" id="removeButton"  ng-click="removeDynamically($index)" type="button">Remove</button>                             
</div> 

答案 2 :(得分:0)

不太确定你要完成什么(因为你的代码看起来像是AngularJS的新手,但我创建了一个不同的(但相似的)实现,它应该适合你的需求(它更容易阅读恕我直言并且可扩展):

HTML:

<div class="card ">        
        <form name="add_destination_form" class="col s12" ng-submit="add_destination_form.$valid && addDestination_Details(destination_details)" novalidate>
            <div class="row" ng-repeat="row in rows track by $index">             
                <div class="col s12 m4" >
                    <label for="destination_features1" >Features</label>
                    <textarea  id="destination_features1" name="destination_features1_{{$index}}" ng-model="destination_details.destination_features1[$index]" placeholder="Data Here"  type="text" ></textarea>                        
                </div>
                <button ng-show="show_removebtn" id="removeButton"  ng-click="removeDynamically($index)" type="button">Remove</button>                              
            </div> 

            <div class="col s12 m4">                 
                <button class="waves-effect waves-light btn" ng-click="addDynamically()" type="button">Add More</button>                                
            </div>

            <div class="row">

                <div class="col s12 m4">
                    <button  type="submit">Submit</button>                              
                </div>

            </div>  
        </form>           
    </div>

JavaScript的:

$scope.rows = [{
  "row_num": 0,
  "text": ""
}];

$scope.addDynamically = function (index) {
   console.log(index);
    $scope.rows.push({
        "row_num": index,
        "text": ""
    });
};

$scope.removeDynamically = function (index) {
  $scope.rows.splice(index, 1);
};

Plunker

答案 3 :(得分:0)

您需要添加一行:

$scope.destination_details = {"destination_features1": ['1']};

一开始。

删除元素时还需要正确删除模型:

$scope.destination_details.destination_features1.splice($index, 1);

https://plnkr.co/edit/pe44Moqe7ymegYFTrXJu?p=preview