While engrossed in the incredible book, Mastering Web Development in AngularJS, I stumbled upon this code snippet:
var Restaurant = function ($q, $rootScope) {
var currentOrder;
this.takeOrder = function (orderedItems) {
currentOrder = {
deferred:$q.defer(),
items:orderedItems
};
return currentOrder.deferred.promise;
};
this.deliverOrder = function() {
currentOrder.deferred.resolve(currentOrder.items);
$rootScope.$digest();
};
this.problemWithOrder = function(reason) {
currentOrder.deferred.reject(reason);
$rootScope.$digest();
};
From what I gather, the $rootScope.$digest();
calls are used to inform Angular that the state of the Promise
has been updated.
Am I correct in my interpretation? Also, are these $rootScope.$digest();
calls necessary in this context?