Skip to content

Instantly share code, notes, and snippets.

@flipjs-sublime
Last active August 29, 2015 14:06
Show Gist options
  • Save flipjs-sublime/d6e73a6b32d8d771b855 to your computer and use it in GitHub Desktop.
Save flipjs-sublime/d6e73a6b32d8d771b855 to your computer and use it in GitHub Desktop.
Angular: Promise
angular.module('app', [])
.service('UserService', function($http, $q) {
this.users = []
this.user = {}
function getData(obj, prop, id) {
var q = $q.defer()
var url = '/users/'
$http.get(url + id).success(function(response) {
obj[prop] = response
q.resolve()
})
return q.promise
}
this.all = function() {
getData(this, 'users')
}
this.get = function(id) {
getData(this, 'user', id)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment