Created
January 2, 2014 06:34
-
-
Save fannheyward/8215728 to your computer and use it in GitHub Desktop.
Angular: make a service with $http.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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