1

I don't know how I access the error message from the service. I'm using WebAPI2 and AngularJS.

Controller:

testApp.controller('ModalCtrl', ['$log', '$scope', '$modalInstance', '$rootScope', 'CrudService',
    function ($log, $scope, $modalInstance, $rootScope, CrudService) {
    $scope.updateItem = function (updateItem) {
       CrudService.update(updateItem)
          .success(...)
          .error(function (data) { //doesn't work: CrudService.update(...) is undefined
              $scope.dataError = data.ModelState 
          });
       $scope.ok();
    }

Service:

testApp.factory('CrudService', ['$log', 'resService',
    function ($log, resService) {
    return {
       ...
       update: function (updateItem) {
          updateItem.$update().then(function (response) {
              //SUCCESS MESSAGE definieren
          }, function (response) {
              $log.info('Update ERR:', response.data.ModelState); //Here I'm getting the error messages
              var dataError = []; 
              dataError = response.data.ModelState; //How can I use this in the Ctrl?
          });
       },
    ...

Resource service:

return {
   name: $resource(baseUrl + '/api/name/:Id', {
      Id: '@Id'
   }, {
      'update': {
         method: 'PUT'
      }
   }),

I want to use the variable "dataError" as $scope in the Ctrl. How can I do this?

1
  • @gr3g This doesn't work. I've tried it before. Commented Jul 6, 2015 at 11:02

1 Answer 1

1
return {
   ...
   update: function (updateItem) {
      return updateItem.$update();
   },

$scope.updateItem = function (updateItem) {
   CrudService.update(updateItem).then(
      function(resp){

      },
      function(error){
        $scope.dataError = error.data.ModelState;
     }

   );
}

UPDATE

Factory (unlike service) isn't returning something automatically.
You need to return like that. (or return your function wich contains another return)

testApp.factory('CrudService', ['$log', 'resService',
    return {
       ...
       update: function (updateItem) {
          updateItem.$update().then(function (response) {
              //SUCCESS MESSAGE definieren
          }, function (response) {
              $log.info('Update ERR:', response.data.ModelState); //Here I'm getting the error messages
              var dataError = []; 
              dataError = response.data.ModelState; //How can I use this in the Ctrl?
          });
       },
    ...
Sign up to request clarification or add additional context in comments.

10 Comments

I'm not 100% sure, but if you want the response of your promise, you should return your promise to have it in your controller. Afther that you'll be able to handle cases and display them
This I've also tried before and it doesn't solved the problem.
How can I define this? I mean CrudService is injected in my Ctrl
Copy paste more code, I can't find the solution here
I've modified my post. I think that's all what is necessary.
|

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.