How to show error message with state resolve object


#1

I’m am using the state resolve object in order to retrieve http results inside my controller.
This works ok but if the http response sends an error message , how do I capture and display it.

.state('main.skill', {
        url  : '/skill',
        views: {
          'mainContent': {
            templateUrl: 'modules/skill/skillList.html',
            controller : 'SkillListCtrl as skillListCtrl',
            resolve: {
              list : function(SkillFactory) {
               return SkillFactory.list();
              }
            }
          }
        }
      })

#2

This is how I solved it. (not sure if that’s most efficient way)

 resolve    : {
              list: function(SkillFactory) {
                return SkillFactory.list()
                  .then(
                  //success
                  function(result) {
                    return result;
                  },
                  //error
                  function(result) {
                    return result;
                  });
              }
            }

controller

angular.module('skillMobile')
  .controller('SkillListCtrl', function(list, $state) {
    var vm = this;
    // todo: check for safer condition then "isArray"
    angular.isArray(list) ? vm.skills = list : vm.error = list;

    vm.showSkillView = function(id) {
      $state.go('main.skillView', {skillId: id});
    }
  });

#3

you can check for the error in the

        // this code handles any error when trying to change state.
        $rootScope.$on('$stateChangeError',
            function (event, toState, toParams, fromState, fromParams, error) {
                console.log('$stateChangeError ' + error && error.debug);

                // if the error is "noUser" the go to login state
                if (error && error.error === "noUser") {
                    $state.go('app-login', {});
                }
            });
    });