Skip to content

Instantly share code, notes, and snippets.

@timestep
Last active August 29, 2015 14:22
Show Gist options
  • Save timestep/eb04628c5268cb64b1fb to your computer and use it in GitHub Desktop.
Save timestep/eb04628c5268cb64b1fb to your computer and use it in GitHub Desktop.
//to use call getOrders.sum().then(function(sum){console.log(sum)});
angular.service('getOrders',function ($http,$q) {
return {
sum: function () {
var d = $q.defer();
//can add try method here
$http.get('/orders').success(function (data) {
//assuming data is just json array of orders.
var orders = data;
var sumOfOrders = 0;
orders.forEach(function (order) {
sumOfOrders += order.value;
});
return d.resolve(sumOfOrders);
}).error(function (res) {
return d.reject(res);
});
return d.promise;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment