Code
angularjs
angular.module('httpApp', [])
.controller('HttpController', function($scope, $http) {
$scope.users = [];
$scope.loading = false;
$scope.error = null;
$scope.loadUsers = function() {
$scope.loading = true;
$http.get('/api/users')
.then(function(response) {
$scope.users = response.data;
})
.catch(function(error) {
$scope.error = 'Failed to load: ' + error.status;
})
.finally(function() {
$scope.loading = false;
});
};
$scope.addUser = function(user) {
$http.post('/api/users', user)
.then(function(res) { $scope.users.push(res.data); });
};
// Config object form with params and headers
$http({
method: 'GET',
url: '/api/users',
params: { page: 1, limit: 20 },
headers: { 'Authorization': 'Bearer token' }
});
});