Skip to content

Instantly share code, notes, and snippets.

@MeirKriheli
Last active August 29, 2015 14:08
Show Gist options
  • Save MeirKriheli/3b1c998951504957e8d6 to your computer and use it in GitHub Desktop.
Save MeirKriheli/3b1c998951504957e8d6 to your computer and use it in GitHub Desktop.
Using promises with angular
angular.module('example')
.factory('Users', function($q, $http) {
var users = null;
return {
get: function() {
deferred = $q.defer();
if (users) {
deferred.resolve(users);
}
else {
$http.get('/api/users/').then(
function(res) {
users = res.data;
deferred.resolve(users)
},
function(err) {
deferred.resolve(err);
}
)
}
return deferred.promise;
}
};
}).
controller('exampleCtrl', function($scope, Users) {
Users.get().then(function(users) {
$scope.users = users;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment