Skip to content

Instantly share code, notes, and snippets.

@aysbg
Last active August 29, 2015 14:18
Show Gist options
  • Save aysbg/ff8a1792047d4a4a0ccd to your computer and use it in GitHub Desktop.
Save aysbg/ff8a1792047d4a4a0ccd to your computer and use it in GitHub Desktop.
http example
(function () {
'use strict';
var UserService = [
'$http', '$q',
function ($http, $q) {
return {
getOne: getOne
};
// Get one user
function getOne(userId) {
var request = $http({
method: 'GET',
url: Config.apiRoute + '/users/' + userId
});
return request.then( handleSuccess, handleError );
}
// Private methods
// -------------------------
// Handle error part of promise callback
function handleError(response) {
// if response is not object or response object does not have message param
if (!angular.isObject(response) || !response.message) {
// return unknown eror
return $q.reject('An unknown error occurred.');
}
// Otherwise, use expected error message.
return $q.reject( response.message );
}
// Handle success part of promise callback
function handleSuccess(response) {
return response.data;
}
}
];
angular.module('yourApp').factory('UserService', UserService);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment