Skip to content

Instantly share code, notes, and snippets.

@GavukaAlexandr
Last active September 29, 2017 19:01
Show Gist options
  • Save GavukaAlexandr/2d19cc734c01e461f1741cd4536cd51a to your computer and use it in GitHub Desktop.
Save GavukaAlexandr/2d19cc734c01e461f1741cd4536cd51a to your computer and use it in GitHub Desktop.
AngularJs Service
angular.module('starter.controllers', [])
.factory('userData', function ($http, $q) {
var userId;
return {
set: function (data) {
userId = data;
},
get: function () {
var deferred = $q.defer();
$http.get('https://api.github.com/user/' + userId, {
params: {
client_id: '9ce0acce7ef3194558c7',
client_secret: '2ecd08db5e0ed67e2a71e8c371a46509b33c4ce2'
}})
.then(function (response) {
deferred.resolve(response.data);
});
return deferred.promise;
}
};
})
.controller('usersController', ['$scope', '$http', 'userData',
function ($scope, $http, userData) {
$scope.getUserData = function (userId) {
userData.set(userId);
};
}])
.controller('userController', ['$scope', '$http', 'userData',
function ($scope, $http, userData) {
userData.get().then(function (data) {
$scope.user = data;
})
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment