我愿意检索资源请求的响应头,因为我在其中放入了分页信息和其他内容而不是响应主体,以使REST api清晰.
虽然我们可以从成功/错误回调中获取它,如下所示:
Object.get({type:'foo'}, function(value, responseHeaders){
var headers = responseHeaders();
});
Run Code Online (Sandbox Code Playgroud)
'Object'是我的资源工厂服务.
此外,当我尝试在解决所需资源后更改路由时,我尝试过:
.when('/list', {
templateUrl: 'partials/list.html',
controller: 'ListCtrl',
// wait for the required promises to be resolved before controller is instantialized
resolve: {
objects: ['Object', '$route', function(Object, $route){
return Object.query($route.current.params).$promise;
}]
}
})
Run Code Online (Sandbox Code Playgroud)
并且在控制器中,只需注入"对象"而不是对象服务,因为它已经解析并填充了真实数据.
但是当我尝试从控制器中的"对象"获取头信息时,我遇到了问题.
我试过了objects.$promise.then(function(data, responseHeaders){}),但是responseHeader是未定义的.
如何更改$ resource服务的行为,以便将responseHeader getter抛出到$ promise then()回调函数中?
我的服务"对象"供参考:
myServices.factory('Object', ['$resource',
function($resource){
return $resource('object/:id', {id: '@id'}, {
update: {method: 'PUT'},
});
}
]);
Run Code Online (Sandbox Code Playgroud)