Skip to content

Instantly share code, notes, and snippets.

@steve228uk
Created July 11, 2014 09:26
Show Gist options
  • Save steve228uk/5a8401f766f2673f9bff to your computer and use it in GitHub Desktop.
Save steve228uk/5a8401f766f2673f9bff to your computer and use it in GitHub Desktop.
$q example
.factory('ourFactory', function($http, $q){
return {
update: function(){
var defer = $q.defer(); // we're gonna create a new promise to return
$http.get('http://google.com').success(function(data){
d.resolve(data); // This is what we can access from our promise later
});
return d.promise; // return the promise
}
};
});
.controller('name', function(ourFactory){
ourFactory.update().then(function(data){ // that data we resolved into our promise in the factory is accessible now
$scope.something = data; //now you can bind that data to your scope
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment