[英]How to remove an element from an array at particular position in angularjs/javascript
在我的情况下,我有一个表单,当我点击添加按钮功能和图像字段行将动态添加,我可以添加数据。现在我想删除某个位置的特定行。我尝试使用拼接切片和弹出.But pop只删除最后一行。当我使用切片和拼接时,删除行的数据不会清除,而行本身也不会清除。这是我的代码如下
$scope.rows = [];
$scope.current_rows = 1;
$scope.destination_details = {};
$scope.rows.push({
row_num: $scope.current_rows
});
// code for adding a row dynamically
$scope.add = function () {
$scope.rows.push({
row_num: $scope.current_rows + 1,
});
};
//code for removing row dynamically
$scope.remove = function ($index) {
$scope.rows.splice({
row_num: ($index, 1),
});
$scope.destination_details.destination_features1[$index] = '';
$scope.destination_image_arr[$index] = '';
};
当我使用:
$scope.rows.slice({
row_num: ($index, 1),
});
它正在清除该特定位置的数据,但该行未关闭。 当我使用:
$scope.rows.pop({
row_num: $scope.current_rows - 1,
});
只有最后一行正在删除。 这是我的HTML代码:
<div class="row" ng-repeat="row in rows track by $index">
<div class="col s12 m4">
<label>Upload Images </label>
<div class="input-field col s11">
<input type="file" id="image" name="image_{{$index}}" ng-files="get_files($files,$index)" ng-model="destination_details.image[$index]" ng-required="true" multiple/>
<div ng-messages="add_destination_form['image_' + $index].$error" ng-if="add_destination_form['image_' + $index].$dirty || add_destination_form.$submitted">
<div ng-message-exp="['required']">
<span class="error">Please add alteast one image !</span>
</div>
</div>
<div ng-if="destination_image_arr[$index].length > 0" id="post_img">
<a ng-click="uncheck_files($index)" title="close" style="cursor:pointer" class="close_image_upload">X</a>
<img class="thumb" ng-src="{{destination_image_arr[$index]}}">
</div>
<div id="img_err_msg"></div>
<br><br><br>
</div>
</div>
<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="Mandatory" type="text" ng-required="true"></textarea>
<div ng-messages="add_destination_form['destination_features1_' + $index].$error" ng-if="add_destination_form['destination_features1_' + $index].$dirty || add_destination_form.$submitted">
<div ng-message-exp="['required']">
<span class="error">Description is required</span>
</div>
</div>
</div>
<div class="col s12 m4">
<button ng-show="show_removebtn" id="removeButton" class="waves-effect waves-light btn" ng-click="removeDynamically($index)" type="button">Remove</button>
</div>
</div>
<div class="col s12 m4">
<button class="waves-effect waves-light btn" ng-click="addDynamically()" type="button">Add More</button>
</div>
任何帮助将不胜感激。谢谢
如果你想拼接一个特定的位置。 这是参考Array.prototype.splice()
var array = [1, 2, 3, 4] array.splice(2, 1); console.log(array);
此处位置2已被删除,它将在第二个参数中删除1次定义。
在您的代码中尝试执行此操作:
$scope.rows.splice($index, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.