AngularJS 1.6.x

In the controller I write:

var Vehicle = $resource(prfx+'vehicles/:Id', {Id:'@id'}); var vehicles = Vehicle.query( function(){ $scope.list = vehicles; $scope.selected = [vehicles[vehicles.length-1]]; } ); 

The choice of an element for editing did this:

 <select ng-model="selected"> <option ng-repeat="option in list | orderBy:'id' track by option.id" ng-value="option"> #{{option.id}} {{option.title}}</option> </select> 

In the template you want to write simple buttons

 <form ng-repeat="option in selected " ng-submit="option.$save()" > формочки <button type="submit">Сохранить</button> <button type="button" ng-click="option.$delete()">Удалить</button> </form> 

The DELETE method is executed on the server and it seems that the resource can be deleted from the model. But he does not delete himself.

Do I need to add my delete handler or can I call this method so that everything works?

    1 answer 1

    So it just will not work, because operations on a single object do not affect the list in which it is located.

    To solve it, it is worthwhile to get a function that accepts a specific option, and in this function, if $delete is successfully completed, you can delete an option from the list.

    For example:

     $scope.removeOption = function removeOption(list,option){ option.$delete(function success(){ // удаление объекта option из нужного списка }); } 

    and call in markup

     <button type="button" ng-click="removeOption(list,option)">Удалить</button> 
    • @eri, in general, it is not necessary to transmit the list, in most cases it will be available through $scope , unlike the selected option - Grundy
    • I moved this function to a separate factory. there is no osprey. In the same factory I will put all standard operations with the list - eri
    • @eri, the answer does not specifically indicate how to remove from the list. There may be several deletion options, plus the selected option may not be in the list, so you should not add a specific implementation. In addition, success is written intentionally, since this callback is exactly for the successful completion of the function $delete - Grundy
    • the word "For example" asked me to add an example.) - eri
    • @eri, here is just an example of a function in which everything happens and an example of the fact that in $delete you can pass callbacks, you can also pass a second parameter to the handler for erroneous completion. - Grundy