Ajax service
by kshep92
HTML
<div ng-app="something">
<div ng-controller="AppCtrl">
<button ng-click="sendRequest()" ng-disabled="processing.active">
Send Request
</button>
</div>
</div>
JavaScript
angular.module("something", [])
.service('ajax', function($q, $timeout, $http) {
var deferred;
var async = { active: false };
function logResult(response, status) {
console.log(response, status)
}
function get(url, successCb, errorCb) {
var success = successCb || logResult;
var error = errorCb || logResult;
async.active = true;
$http.get(url).then(success, error).finally(function() { async.active = false; });
}
return { get: get, async: async }
})
.controller("AppCtrl", function($scope, ajax) {
$scope.sendRequest = function() {
$scope.processing = ajax.async;
ajax.get('https://api.github.com/repos/jooby-project/jooby', function(res) { alert(res.status); });
};
})