Skip to content

Instantly share code, notes, and snippets.

@fannheyward
Created January 2, 2014 06:34
Show Gist options
  • Save fannheyward/8215728 to your computer and use it in GitHub Desktop.
Save fannheyward/8215728 to your computer and use it in GitHub Desktop.
Angular: make a service with $http.
app.factory('myService', function($http) {
var myService = {
async: function() {
// $http returns a promise, which has a then function, which also returns a promise
var promise = $http.get('test.json').then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response);
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return myService;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment