(simple plunkr)
$http, in fact, returns a promise. Also, a good pattern is to first handle the returned promise in the service for any data related actions and then pass that promise up the 'chain' to the controller where UI actions can be handled. I have included a simplified plunkr that shows how these promises are passed from the service to the controller. Just click on the 'get data' button in the controller and you'll see how the events work with a series a 'alert()s'. Note, I have used $timeout as a substitute for $http. The promise handling will be identical using $http. More details below:
index.html: Starts everything. The get data button invokes getData()
method in the controller.
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
<script src="mock_ajax_service.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Click on button to request data and handle the promise chain</p>
<div>
<button ng-click="getData()">Get Data!</button>
</div>
</body>
</html>
app.js: getData(), in turn, invokes the requestData() method in the 'mock ajax'
service. It waits on a promise to return from the service and upon
return handles UI matters, i.e. shows an alert.
var app = angular.module('plunker', ['mockjaxservice_module']);
app.controller('MainCtrl', ['$scope', 'mockajaxservice',
function($scope, mockajaxservice) {
$scope.name = 'World';
var requestDataSuccess = function (results) {
console.log('handle success data in controller/UI');
alert('handle success data in controller/UI');
return;
}
var requestDataFail = function (results) {
console.log('handle failed data in controller/UI');
return;
}
$scope.getData = function () {
var UIPromise = mockajaxservice.requestData();
UIPromise.then(requestDataSuccess,requestDataFail);
}
}]);
mock_ajax_service.js: Here, the service simulates a mock ajax call
using $timeout which is invoked by the controller button. $timeout,
much like $http, returns a bonafide promise. Once the promise
returns, it is handled (an alert), and the promise is passed up the
chain to the controller (return that;)
'use strict';
var mockAjaxModule = angular.module('mockjaxservice_module', []);
mockAjaxModule.factory('mockajaxservice', ['$timeout', function ($timeout) {
this.service_count = 0;
this.service_count_object = {
count:0
}
var that = this;
var requestDataSuccess = function (results) {
console.log('handle success data in service');
alert('handle success data in service then pass it up to controller');
that.service_count = 10;
that.service_count_object.count = 10;
that.utcTime = Math.floor((new Date()).getTime() / 1000);
return that;
}
var requestDataFail = function (results) {
console.log('handle failed data in service')
return that;
}
this.requestData = function () {
var requestDataPromise = $timeout(function() {
}, 500);
requestDataPromise.then(requestDataSuccess,requestDataFail);
return requestDataPromise;
}
return this;
}]);