#AngularJS I have a table. When I click the DELETE button on any row, the following code always deletes the first one. Where am I wrong? I'll post part of the code
<tr id="riga" ng-repeat='x in cars'>
<td >{{x.id}}</td>
<td >{{x.targaauto}}</td>
<td >{{x.datiintestatario}}</td>
<td >{{x.marca}}</td>
<td><button type="button" class='btn btn-danger btn' data-bs-toggle="modal" data-bs-target="#finestra"> elimina</button></td>
</tr>
<div id="finestra">
<div >
<div >
<div >
<h5 >Attenzione</h5>
<button type="button" data-bs-dismiss="modal"></button>
</div>
<div >
<h5 >Sei sicuro di voler eliminare questo record?</h5>
</div>
<div >
<button type="button" ng-click='rimuovi($index)' data-bs- dismiss="modal">OK</button>
<button type="submit" data-bs-dismiss="modal">ANNULLA</button>
</div>
</div>
</div>
</div>
</table>
</tbody>
<script src="bootstrap.js"></script>
</body>
</html>
Function RIMUOVI
$scope.rimuovi=function($index){
$scope.cars.splice($index, 1)
}
CodePudding user response:
When you press the
<button type="button" class='btn btn-danger btn' data-bs-toggle="modal" data-bs-target="#finestra"> elimina</button>
button, you need to add
ng-click="verifyDelete($index)"
which you will save in your directive/controller. That way, when you ask "Are you sure" from the modal, you have the correct row.
Update ng-click='rimuovi($index)'
to ng-click='rimuovi()'
and use the value saved from verifyDelete
CodePudding user response:
I'm sorry but I did not understand. I have recently started the study of AngulaJS. Do I need to create a new function to insert into the controller? What should the function contain?