gpt4 book ai didi

javascript - 如何从 angularjs/javascript 中特定位置的数组中删除元素

转载 作者:行者123 更新时间:2023-11-30 09:32:05 25 4
gpt4 key购买 nike

在我的例子中,当我单击添加按钮时,我有一个表单,特征和图像字段行将动态添加,我可以添加数据。现在我想删除某个位置的特定行。我尝试使用 splice slice 和 pop .但 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);

关于javascript - 如何从 angularjs/javascript 中特定位置的数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45673813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com