如何从angularjs/javascript中特定位置的数组中删除元素
在我的例子中,我有一个表单,当我单击“添加”按钮时,功能和图像字段行将动态添加,我可以添加数据。现在我想删除某个位置的特定行。我尝试使用“拼接切片”和“弹出”。但“弹出”仅删除最后一行。当我使用“切片”和“拼接”时,删除行的数据不会被清除,行本身也不会被清除正在清除。下面是我的代码如何从angularjs/javascript中特定位置的数组中删除元素,javascript,angularjs,Javascript,Angularjs,在我的例子中,我有一个表单,当我单击“添加”按钮时,功能和图像字段行将动态添加,我可以添加数据。现在我想删除某个位置的特定行。我尝试使用“拼接切片”和“弹出”。但“弹出”仅删除最后一行。当我使用“切片”和“拼接”时,删除行的数据不会被清除,行本身也不会被清除正在清除。下面是我的代码 $scope.rows = []; $scope.current_rows = 1; $scope.destination_details = {}; $scope.rows.push({ row_num
$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,
});
它正在清除该特定位置的数据,但该行没有关闭。
当我使用:
$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>
上传图像
请添加一张图片!
X
特征
需要说明
去除
添加更多
任何帮助都将不胜感激。如果您想拼接特定位置,请多谢。这是参考资料
var数组=[1,2,3,4]
阵列拼接(2,1);
console.log(数组)代码>如果要拼接特定位置。这是参考资料
var数组=[1,2,3,4]
阵列拼接(2,1);
console.log(数组)代码>当我使用splice时,所有的行都在删除,数据也不在调用remove方法的地方清除。我在button click中看到RemoveDynamicly()被调用。是的,我在button click操作中删除它。如果它还没有解决,你能发布你的最新代码吗?我认为您可以从按钮中删除id属性。这是ng-repeat中id相同的按钮。是的,这是我的代码:当我使用splice时,所有行都在删除,数据也不在调用remove方法的位置清除。我在button click中看到RemoveDynamicly()被调用。是的,我在button click操作中删除它。如果它还没有解决,你能发布你的最新代码吗?我认为您可以从按钮中删除id属性。它在ng-repeat中使用相同id的按钮。是,这是我的代码:$scope.rows.splice($index,1)代码>应该可以工作。$scope.rows.splice($index,1)代码>应该可以工作。