AngularJS ngResource delete event

偶尔善良 提交于 2020-01-03 11:58:11

问题


I'm trying to keep a list of resources up to date as a user interacts with it. Using an AngularJS ngResource I initially grab the list using it's query method. Each resource then has a $remove (or $delete) method, right? But when fired, the resource isn't removed from the list returned from query.

This is asking a lot, I know, but I was almost hoping it would just do everything for me. Save that, how could I accomplish this. Does the resource itself emit some kind of event? Does it have a deleted property I can $watch? How would I go about know that a resource was $remove'd so I can splice it out of the list?

Thanks.


回答1:


You have to use Array's splice method to remove it ($index is ng-repeat's implicit index).

$scope.removeItem = function (index) {
    $scope.items[index].$delete();
    $scope.items.splice(index, 1);
}

And then in your HTML

<a ng-click="removeItem($index)">remove me</a>



回答2:


Just use the success callback function:

instance.$action([parameters], [success], [error])

For you I am guessing that would be something like:

myResource.$delete([parameters], function () {
    //delete was successful
});


来源:https://stackoverflow.com/questions/16700788/angularjs-ngresource-delete-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!