我有以下资源:
factory('User', function($resource) {
return $resource(apiURL + 'user/:userId', {}, {
query: {
method: 'GET',
isArray: true,
transformResponse: function(data, headers) {
data = JSON.parse(data);
var cursor = headers().link;
if(!cursor) {
return data;
}
data.cursor = getNextCursor(cursor);
console.log(data);
return data;
}
}
});
}).
这是我的控制器:
controller('UserListCtrl', function($scope, $rootScope, User) {
$scope.users = [];
$scope.orderBy = 'created';
$rootScope.title = 'user';
var cursor;
/**
* Appends new results to the list of users.
*/
function appendResults(results) {
console.log(results);
cursor = results.cursor;
console.log('set cursor: ' + cursor);
for(var i in results) {
if(results[i] instanceof User) {
$scope.users.push(results[i]);
}
}
}
/**
* Fetches new users from the server and appends it to the
* locally stored users.
*/
$scope.fetch = function() {
if(cursor) {
console.log('using cursor:');
User.query({ncursor: cursor}).$promise.then(appendResults);
} else {
User.query().$promise.then(appendResults);
}
};
$scope.fetch();
}).
我需要将响应标头中的光标从资源传递到我的控制器。有没有(干净的)方法可以做到这一点?