Create a gist now

Instantly share code, notes, and snippets.

@aysbg /httpexample
Last active Aug 29, 2015

What would you like to do?
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