问题标签 [angular-resource]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
19495 浏览

angularjs - 如何删除 AngularJS 中特定 XHR 请求的默认标头?

我 99% 的 ajax 调用需要一个特定的“X-API-TOKEN”来验证我的 Rails REST API 并与之通信。但我也在调用一个第三方 API,并且一直收到错误消息,提示“Access-Control-Allow-Headers 不允许请求标头字段 X-API-TOKEN”。

如果我在通话之前删除标题,一切正常,解决方法是删除然后在通话后重新添加,但有没有比这更简单的方法:

0 投票
0 回答
53 浏览

rest - 具有自定义操作和 url 的 $resource 不起作用

我想将 $update 方法添加到 Todo 对象。这是我的代码:

当我查看控制台时,我收到了关于 todosjson?destinataire_id=?id=81 的 PUT 请求

有什么问题?

0 投票
1 回答
920 浏览

angularjs - ng-resource url 参数不起作用

$resource 在通过自定义操作发出 PUT 请求时未正确传递 url 参数。

这是我创建资源的服务。

在我的控制器中,我试图更新这样的项目列表。注意$scope.cart.id存在并且是正确的(在这种情况下为1)

但是请求 URL 是:/api/cart/items/但我期待/api/cart/1/items/. 如果我这样做,这可以正常.get({cartId: <some_id>})工作,但似乎不适用于更新。

编辑:角度版本 1.1.5

0 投票
2 回答
6870 浏览

angularjs - $resource 并获取数组

我有一个 API,它返回以下形式的数据(使用tastepie):

所以我认为要让我的资源正常工作,我应该使用:

但是当我将 isArray 设置为 true 时:我收到以下错误:

如果我将 isArray 设置为 false,我没有错误,但我的对象还包含此请求的所有元数据...

不确定我是否正确理解了这个 $resource 功能

0 投票
1 回答
708 浏览

angularjs - ServiceStack:使用 DTO 中的属性来设置响应头和响应体

我正在使用带有 AngularJS 资源模块的 servicestack。问题是,当我调用服务的 query() 方法来请求分页列表时,我想发送一个包含总行数的自定义响应标头。

我希望 http 响应看起来像这样:

目前,为此,我使用以下 DTO:

在服务中:

我想在 servicestack 中做的是在 ResponseDTO 中使用自定义属性来指示我希望在响应标头中序列化某些属性,并在响应正文中序列化其他属性。响应 DTO 可能如下所示:

该服务将创建如下响应:

当然,HTTP 响应看起来与上面完全相同。

所以我的问题是:是否可以使用自定义属性来实现这一点?

0 投票
2 回答
1199 浏览

javascript - AngularJS/ JS 如何访问函数外部的变量?

我正在尝试访问从功能块中定义的变量 np;但是,当我调用this.items.push(plumbers). 我明白了TypeError: Cannot call method push of undefined

0 投票
2 回答
1181 浏览

angularjs - Angular $resource 和使用拦截器来检查响应/错误/回调

最近几天,我正在努力调用 REST 服务并跟踪响应、错误、回调等。我已经阅读了大部分帖子,但是由于我对 Angular 的理解有限,我似乎无法理解它。以下是我到目前为止的问题和理解。

我正在使用仅返回“project_id”的 Project.$update() 服务。此服务器不再返回完整数据。以下是在这里分享的几行代码。

以下是我用来更新/创建项目的指令中的代码。

以下是 save() 和 update() 的服务响应。

这里的问题是;“项目”对象值被服务器返回的新响应替换,只有 project_id 和其他字段被替换。

我正在阅读有关 $resource 的详细文档,但我无法掌握它。在这里获得一些指导来编写代码来检测错误、响应、回调会很棒。

0 投票
1 回答
3443 浏览

angularjs - 角度查询中的嵌套参数

我有一个基本的角度资源(角度 1.0.7):

我可以传递如下参数:

但我需要传递嵌套参数:

这失败了,因为它发送:

我试过了:

但显然,服务器端无法识别参数(仅仅是字符串),我想避免在那里解析的麻烦。

你对这个问题有一个优雅的解决方案吗?


使用 jQuery 我已经完成了:

生产:

由服务器完美解释。


似乎是一个已知问题这里也是),让我们从现在开始坚持一个丑陋的补丁......

0 投票
1 回答
1629 浏览

angularjs - Items disappear when saved

I created a directive to display data. I can include it in an ng-repeat and see that the model is connected. When I click away and return the page, the data is displayed again with previously made changes. I used ngResources to load the data from an API. So far, so good: it works as expected.

Here's the part that breaks: if I save the data to the API in my directive, the item disappears from the page (more specifically, it's drawn with its fields blank). The rest of the items remain as before. Only the saved one changes.

Here's the directive:

The html associated with the directive includes:

Here's how the directive used:

Here's a fiddle that shows additional context. You can see the basic directive working with some fake data. The code for the API call is commented out.

Again, the problem occurs when the scope.data.$save(); line is uncommented. When the item displayed in the ng-repeat tries to save itself, it disappears from the displayed list of items, replaced by one with blank fields. When I reload the whole application, I see that the API save succeeded and the item now displays correctly.

What I suspect is happening is that the save call operates on the correct item but somehow replaces the changed item with a new, blank one, perhaps due to prototypal inheritance. The save then causes an update to the parent scope, which redraws the list and shows the new blanked item.

Can someone clarify what is happening and how to fix it?

0 投票
0 回答
448 浏览

angularjs - angular.forEach 中的异步 POST 请求

在 .forEach 中发出异步发布请求给我带来了很多问题。无论是否使用 $resource 我都会丢失数据。或者服务器可能无法同时处理所有请求,我真的不知道。

这是我正在使用的代码:

我只能得到一个正确的结果,其余的都是不完整的。我使用 jquery ajax 和 async to false 来解决这个问题,但我想避免使用 jquery 并以有角度的方式解决这个问题