I have an Angular controller and an Angular service. From my controller, I'm calling to the service.There are two functions in my Service. First function works fine. But this problem occurs with the second one. Following is the code for the relevant function.
function getCurrentUser() {
return $resource(_URLS.BASE_API + 'get_current_user' + _URLS.TOKEN_API + $localStorage.token).get().$promise;
}
Following the code for calling that service's function from the controller,
//Get current user id
$scope.getCurrentUserId = function() {
ParentService.getCurrentUser.then(function(response) {
if (response.query_status == "success") {
$scope.userid = response.data.id;
console.log('Value is: '+ $scope.userid);
} else {
$scope.userid = null;
}
}, function(error) {
$scope.userid = null;
console.log("Error : Failed to load user data...");
console.log(error);
});
};
I can't figure out what the issue is since my first function works fine. So hope someone will help me to solve this issue.
Your controller code should be as following. Notice the ()
in ParentService.getCurrentUser()
$scope.getCurrentUserId = function() {
ParentService.getCurrentUser() // notice the "()" here
.then(function(response) {
...
}, function(error) {
...
});
};