使用 ngResource 访问 headers
access headers with ngResource
我有一个 Api,其中检索自定义 header: X-Total-Count : "total of items",我正在使用 angular 和 ngResource
.
我的工厂是这样的:
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" })
};
});
当我调用它时:
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);
});
如何通过 ngResource 访问我的自定义 header,我可以访问它但是使用 $http,我想使用 $resource 方式,谢谢
可以使用三个参数调用 query
操作方法:
Resource.query([parameters], [success], [error])
使用 (value (Object|Array), responseHeaders (Function), status (number), statusText (string))
个参数调用成功回调,其中值是填充的资源实例或集合对象。使用 (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);
}
);
有关详细信息,请参阅 AngularJS $resource Service API Reference。
whats the difference between using $promise.then
and the success function,
.then
方法中的函数仅公开最终响应的 value
。虽然成功回调公开了四个参数:value (Object|Array), responseHeaders (Function), status (number), statusText (string)
.
$promise
可以作为参数传递给其他函数,其.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.
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,其中检索自定义 header: X-Total-Count : "total of items",我正在使用 angular 和 ngResource
.
我的工厂是这样的:
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" })
};
});
当我调用它时:
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);
});
如何通过 ngResource 访问我的自定义 header,我可以访问它但是使用 $http,我想使用 $resource 方式,谢谢
可以使用三个参数调用 query
操作方法:
Resource.query([parameters], [success], [error])
使用 (value (Object|Array), responseHeaders (Function), status (number), statusText (string))
个参数调用成功回调,其中值是填充的资源实例或集合对象。使用 (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);
}
);
有关详细信息,请参阅 AngularJS $resource Service API Reference。
whats the difference between using
$promise.then
and the success function,
.then
方法中的函数仅公开最终响应的 value
。虽然成功回调公开了四个参数:value (Object|Array), responseHeaders (Function), status (number), statusText (string)
.
$promise
可以作为参数传递给其他函数,其.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.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.