0

The following service are invoked in the controller:

//Controller
$scope.groups = CrudService.getAllGroups();

This service returns all groups from the database. So next, as shown below, the service code:

//Service (CrudService)
function getAllGroups() {
  return ResService.group.query(
      successResponse,
      errorResponse);
}

function successResponse(resp) {
    return resp;
}

/*This part doesn't work*/
function errorResponse(error) {
    return function (err) {
        $scope.msgError = false;
        $scope.errStatus = err.status;
        $scope.statusText = err.statusText;
        $scope.errMsgDetail = err.data.MessageDetail;
    };
}
/**************************/

//Nested Service (ResService)
return {
   group: $resource(baseUrl + '/api/group/:Id', {
      Id: '@Id'
   }, {}),
}

How you can see in the service code, the error response won't invoked in the view. I'm using the error messages from the response header respectively the backend. If the request failed then the alert box have to displayed as the following code demonstrates:

<div>
   <alert ng-hide="msgError" type="alert alert-danger" close="closeAlert()">
       <p><b>{{ statusTitle }}</b>: <i>{{ errStatus }} - {{ statusText }}</i></p>
       <p><strong>{{ msgTitle }}</strong>: <i>{{ errMsgDetail }}</i> <i>{{ msgException }}</i></p>
   </alert>
</div>

Do anyone has an idea how I can access or correctly define the values in the errorResponse function? Or, is it possible to declare in the service query at all?

2 Answers 2

1

When the error function is called it does nothing but return another error function. Another problem is that it doesn't know the variable $scope in its context, so you have to pass it:

$scope.groups = CrudService.getAllGroups($scope);

function getAllGroups($scope) {
  return ResService.group.query(
      successResponse,
      errorResponse($scope));
}

function errorResponse($scope) {
    return function(error) {
        $scope.msgError = false;
        $scope.errStatus = error.status;
        $scope.statusText = error.statusText;
        $scope.errMsgDetail = error.data.MessageDetail;
    }
}

Also there was a typo, you wrote err., but it should be error..

Sign up to request clarification or add additional context in comments.

1 Comment

thanks.. the correct solution was to send the $scope. thaaaanks!
0

You're passing the object in to a parameter named error, but then you're referencing it as err. It's just a typo.

/*This part doesn't work*/
function errorResponse(error) {
   return function (error) {
      $scope.msgError = false;
      $scope.errStatus = err.status;
      $scope.statusText = err.statusText;
      $scope.errMsgDetail = err.data.MessageDetail;
 };

}

Should be: /This part doesn't work/ function errorResponse(error) { return function (error) { $scope.msgError = false; $scope.errStatus = error.status; $scope.statusText = error.statusText; $scope.errMsgDetail = error.data.MessageDetail; }; }

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.