Using ng-repeat for list items in a ul to repeat the items in an employeePayments array, access via $stateParams and stored in $scope.employeePayments.
Whenever I click on the delete button for an item, depending on the two ways I've tried to set up the splice function, it either deletes the first item or the last item from the array, rather than deleting the item selected by finding it via its index in the array.
First method - Always deletes the first item:
$scope.deleteEmployeePaymentRow = function(employeePayment) {
$scope.employeePayments.splice(employeePayment, 1);
}
Second method - Always deletes the last item:
$scope.deleteEmployeePaymentRow = function(employeePayment) {
var index = $scope.employeePayments.indexOf(employeePayment);
$scope.employeePayments.splice(index, 1);
}
-
ng-repeat HTML:
<tr data-ng-repeat="employeePayment in employeePayments">
<td>{{employeePayment.code}}</td>
<td>{{employeePayment.paymentType}}</td>
etc...
-
Delete button HTML:
<a data-ng-click="deleteEmployeePaymentRow(employeePayment)">Delete Payment</a>
employeePaymentis not in the array, soindexOfreturn-1, making your function deleting the last element. Can you show us the whole HTML part ? You can alsoconsole.log(employeePayment)to see what it is actually.