Luddinus Luddinus - 9 months ago 28
AngularJS Question

Angular - Is possible to get the $http parameters?

short question

I have this:

let promise = $http.get('/api/users');


Can I get the promise's parameters? Something like this:

let promise = $http.get('/api/users');

let parameters = promise.getParameters();

/*

parameters = {
method: 'GET',
url: 'api/users',
// etc.
}

*/


I would like to make a Pagination service where I pass a promise and then I could change the url passing parameters (previous/next page etc.)

Thx.

edit:

I need to explain better:

I'd like to pass the "main" promise to a PaginationService.

this.paginator = {};

Paginator.make(this.paginator, function (page) {
// this will be the "main" promise
// page = 1
return $http.get('/api/users', {
params: {
page: 1
}
});
});

// PaginatorService
this.make = function (scopePaginator, callback) {
scopePaginator.next = function () {
return callback(scopePaginator.current_page);
};

return callback().then(function (response) {
// the response.meta contains all the data (current_page, etc.);

angular.extend(scopePaginator, response.data);

return scopePaginator;
});

}


Something like this is what I'm doing...

Answer

The configuration object that generated the request is part of the response object.

var promise = $http.get('/api/users');

promise.then(function onSuccess(response) {
    var config = response.config;
    console.log(config.method);
    console.log(config.url);
    console.log(config.params);
});

Thus successive $http calls can be chained from the promise.

promiseNext = promise.then(function onSuccess(response) {
    var config = response.config;
    vm.data[config.params.page] = response.data;
    config.params.page++;
    //return promise for chaining
    return $http(config);
};