AngularJS $资源集合中删除项目 [英] AngularJS $resource DELETE item in collection

查看:194
本文介绍了AngularJS $资源集合中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Web API(1)控制器,GET,POST和DELETE操作。我从一个角度1.2.0 RC3应用与 $资源调用此。让我们把控制器 FOOS

I have an ASP.NET Web Api (1) controller with GET, POST and DELETE actions. I am calling this from an Angular 1.2.0 RC3 app with $resource. Let's call the controller Foos.

我这样做,返回FOOS列表的GET:

I do a GET that returns a list of foos:

GET http://localhost:55386/api/foos/123456/1 HTTP/1.1
Host: localhost:55386
Connection: keep-alive
Accept: application/json, text/plain, */*
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,es;q=0.6

在资源为

/api/foos/clientId/recordId

在这里,我要说的让我的客户端x和记录ÿFOOS名单

现在,我想删除从我收到的Foo的列表中的单个富,所以我称之为 $删除

Now, I want to remove a single foo from the list of foos that I received, so I call $delete:

$scope.delete = function(foo){
    foo.$delete();
}

然而,这产生了以下要求:

however this results in the following request:

DELETE http://localhost:55386/api/foos/123456/1 HTTP/1.1
Host: localhost:55386
Connection: keep-alive
Accept: application/json, text/plain, */*
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,es;q=0.6

这删除显然是试图删除FOOS的完整列表,这使得SENCE。

This delete is obviously trying to delete the entire list of foos, which makes sence.

我的问题是,如何删除我采用了棱角分明的$资源单一富没有得到每个富在自己的GET请求?

更新:

我可以做一个 GET / API /富/ 1 其中的资源富/ fooId ,其相当于删除/ API /富/ 1 来删除它,但我想要得到的Foo的一个列表,而不是每个单独的富。

I could do a GET /api/foo/1 where the resource is foo/fooId, and its equivalent DELETE /api/foo/1 to delete it but I want to get a list of foos instead of each foo individually.

推荐答案

我是如何误解$ 资源的作品。我以为,知道如何删除自身,因为它是在下面的函数资源实例:

I was misunderstanding how $resource works. I assumed thatfoo knew how to delete itself as it is a Resource 'instance' in the following function:

$scope.delete = function(foo){
    foo.$delete();
}

正确的做法是:

$scope.delete = function(foo){
    Api.delete({ 
        id: foo.Id, 
        clientId : $scope.clientId, 
        recordId : $scope.recordId 
    });
}

您必须手动告诉 $资源实例使用foo的ID,以便该URL包括foo的ID,并执行以下删除

You have to manually tell the $resource instance to use the id of foo so that the url includes the foo id and does the following DELETE

DELETE http://localhost:55386/api/foos/123456/1/123 HTTP/1.1

,其中123 fooId

这篇关于AngularJS $资源集合中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆