Skip to content

Instantly share code, notes, and snippets.

@chrissearle
Created September 24, 2014 19:10
Show Gist options
  • Save chrissearle/cb584ba9281e13ced8c0 to your computer and use it in GitHub Desktop.
Save chrissearle/cb584ba9281e13ced8c0 to your computer and use it in GitHub Desktop.
AngularJS - service to call http async and return data to controller - is there a better way than exposing the promise itself back to the controller?
.factory('someService', ['$http', function ($http) {
return {
async: function () {
return $http.get('someUrl').then(function (response) {
return response.data;
});
}
};
}]).controller('someController', ['$scope', 'someService', function ($scope, someService) {
someService.async().then(function (d) {
$scope.someData = d;
});
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment