在我的情况下我有一个表单,当我点击添加按钮功能和图像字段行将动态添加,我可以添加数据。现在我想删除某个位置的特定行。我尝试使用拼接切片和弹出。但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>
任何帮助都将不胜感激。谢谢
答案 0 :(得分:3)
如果你想拼接一个特定的位置。这是参考Array.prototype.splice()
var array = [1, 2, 3, 4]
array.splice(2, 1);
console.log(array);
&#13;
此处位置2已被删除,它将删除第二个参数中的1次定义。
在您的代码中尝试执行此操作:
$scope.rows.splice($index, 1);
答案 1 :(得分:0)
试试这个
var array = [1, 2, 3, 4]
console.log('array befor removing elemnt '+array);
var postion = 1;
var numberOFElemntsToRemove = 1;
array.splice(postion, numberOFElemntsToRemove );
console.log('array after removing elemnt '+array);
&#13;
在你的代码中 而不是这个
$scope.rows.splice({
row_num: ($index, 1),
});
使用
$scope.rows.splice($index, 1);