[英]access headers with ngResource
I have a Api in which retrieves a custom header: X-Total-Count : "total of items", I'm using angular with ngResource
. 我有一个Api在其中检索自定义标头:X-Total-Count:“项目总数”,我在
ngResource
使用angular。
My factory look like this: 我的工厂是这样的:
app.factory("shopFactory", function ($resource) {
return {
User: $resource("http://localhost:58495/users/api/User/?id=:id", { id: "@id" }),
Category: $resource("http://localhost:58495/category/:id", { id: "@id" }),
Product: $resource("http://localhost:58495/products/?from=:from&to=:to", { from: "@from", to: "@to" })
};
});
And when I call it: 当我称它为:
var productServer = shopFactory.Product.query({ from: 0, to: 10 }).$promise.then(function (response) {
$scope.product = response;
console.log(response);
}, function (error) {
console.log("ERROR");
console.log(error);
});
How can I access my custom header through ngResource, I can access it but with $http, I want to do it with the $resource way, Thanks 我怎样才能通过ngResource访问我的自定义标头,我可以使用$ http访问它,但是我想用$ resource的方式来实现,谢谢
The query
action method can be called with three arguments: 可以使用三个参数来调用
query
操作方法:
Resource.query([parameters], [success], [error])
The success callback is called with (value (Object|Array), responseHeaders (Function), status (number), statusText (string))
arguments, where the value is the populated resource instance or collection object. 使用
(value (Object|Array), responseHeaders (Function), status (number), statusText (string))
参数调用成功回调,其中值是填充的资源实例或集合对象。 The error callback is called with (httpResponse)
argument. 使用
(httpResponse)
参数调用错误回调。
var productServer = shopFactory.Product.query(
{ from: 0, to: 10 },
function success (value, headers, status, statusText) {
$scope.product = value;
console.log(value);
console.log(headers());
},
function error (error) {
console.log("ERROR");
console.log(error);
}
);
For more information, see AngularJS $resource Service API Reference . 有关更多信息,请参见《 AngularJS $ resource Service API参考》 。
whats the difference between using
$promise.then
and the success function,使用
$promise.then
和成功函数有什么区别,
The function in the .then
method exposes only the value
of the final response. .then
方法中的函数仅公开最终响应的value
。 While the success callback exposes four arguments: value (Object|Array), responseHeaders (Function), status (number), statusText (string)
. 成功回调公开四个参数:
value (Object|Array), responseHeaders (Function), status (number), statusText (string)
。
The $promise
can be passed as an argument to other functions and its .then
method can be invoked multiple times. $promise
可以作为参数传递给其他函数,并且其.then
方法可以多次调用。
Another very important difference is that the .then
method creates a new promise from values returned. 另一个非常重要的区别是
.then
方法从返回的值创建新的.then
。
Chaining promises
连锁承诺
Because calling the
.then
method of a promise returns a new derived promise, it is easily possible to create a chain of promises.因为调用promise的
.then
方法会返回新的派生promise,所以很容易创建promise的链。It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain.
可以创建任何长度的链,并且由于一个承诺可以用另一个承诺来解决 (这将进一步推迟其解决方案),因此可以在链中的任何点暂停/推迟对承诺的解决。 This makes it possible to implement powerful APIs.
这样就可以实现功能强大的API。
— AngularJS $q Service API Reference (Chaining Promises)
— AngularJS $ q服务API参考(链式承诺)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.