In my case I have a form when i click add button the features and image filed row will add dynamically and I can add data.Now I want to remove the particular row at some position.I have tried using splice slice and pop .But pop removes only last row.When I use slice and splice the data of removed row is not clearing and the row itself not clearing.Here is my code below
$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] = '';
};
When i use :
$scope.rows.slice({
row_num: ($index, 1),
});
It is clearing the data at that particular position,but the row is not closing. When i use :
$scope.rows.pop({
row_num: $scope.current_rows - 1,
});
Only the last row is deleting. Here is my HTML code :
<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>
Any help would be appreciated.Thanks
$scope.rows.splice($index, 1);should work.