This code above looks like spaghetti. Here is a basic factory that should do what you want:
app.factory('UserService', [ '$http', function($http){
var userService = {};
userService.getUser = function(){
return $http.get('/api/getuser').then(function(res){
return res.data;
},function(error){
console.log(error);
return [];
});
}
return userService;
}]);
then call it in a controller:
app.controller('MyController', ['$scope', 'UserService', function($scope,UserService){
$scope.user = {};
UserService.getUser().then(function(data){
$scope.user = data.users[0];
});
}]);
This assumes a json format similar to { users: [{ id: "34534534",name: "John" }] }
returned by your API.
Please note that I wrote this on the fly and didn't try it out. It should work though.
Warning: I just edited my code to fix some mistakes.