0

I am trying to call a service method in a controller. Here is my code. ROAService.js:

return{
loadROAValues:function(userId,roaId){
    var params=JSON.stringify({userId:userId,roaId:roaId});
    var promise = $http.post(url+'/'+'getROAItems',params).success(function(data){
        roaDetails = data;
        $log.debug("values in service class");
        $log.debug("values in ROA");
        $log.debug(roaDetails.id);
        return roaDetails
    })
    .error(function(data){
    roaDetails = 'error';
    return roaDetails;
    });
    return promise;
}
}

ROAController.js:

$scope.getROA = function(roaObj){
    var currentROA= {};
    currentROA = ROAService.loadROAValues($scope.getMemberId(),roaObj.id);
    $log.debug("values in controller");
    $log.debug(currentROA.id);

}

in console I am getting these values: values in controller undefined values in service class values in ROA 342 there might be race condition . how to get the values in controller.

2 Answers 2

2

Your service returns a promise so you need to call THEN ...

currentROA = ROAService.loadROAValues($scope.getMemberId(),roaObj.id).then(function(response) {
   console.log(response);
}).catch(function(response) {
   console.log("failure", response);
});
Sign up to request clarification or add additional context in comments.

Comments

1

I think you shouldn't do callback in a service. Return a promise instead and use then and do whatever you want with it in controller function.

return {
    loadROAValues:function(userId,roaId){
        var params=JSON.stringify({userId:userId,roaId:roaId});
        var promise = $http.post(url+'/'+'getROAItems',params);
        return promise;
    }
}

and then

currentROA = ROAService.loadROAValues($scope.getMemberId(),roaObj.id)
.then(function(response) {
   console.log(response);
}, function(response) {
   console.log("error", response);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.