How to handle error in angular-ui-router's resolve

前端 未结 2 1882
闹比i
闹比i 2020-12-13 17:28

I am using angular-ui-router\'s resolve to get data from server before moving to a state. Sometimes the request to the server fails and I need to inform the use

2条回答
  •  -上瘾入骨i
    2020-12-13 18:11

    The issue is that if any of the dependencies in the route resolve is rejected, the controller will not be instantiated. So you could convert the failure to data that you can detect in the instantiated controller.

    Example Pseudocode:-

       data: ["$q", "$timeout","$http", function ($q, $timeout, $http) {
          return $timeout(function () { //timeout already returns a promise
            //return "Yes";
            //return success of failure
             return success ? {status:true, data:data} : {status:false}; //return a status from here
           }, 2000);
         }]
    

    and in your controller:-

     controller: ["$scope", "data", "$state", function ($scope, data, $state) {
          //If it has failed
          if(!data.status){
            $scope.error = "Some error";
           return;
          }
            $scope.detailvm = {
              state: $state.current.name,
              data: data
            };
    

    If you are making an $http call or similar you can make use of http promise to resolve the data always even in case of failure and return a status to the controller.

    Example:-

    resolve: {
            data: ["$q", "$timeout","$http", function ($q, $timeout, $http) {
               return $http.get("someurl")
                 .then(function(){ return {status:true , data: "Yes"} }, 
                        function(){ return {status:false} }); //In case of failure catch it and return a valid data inorder for the controller to get instantated
            }]
          },
    

    "use strict";
    
    angular.module('MyApp', ["ui.router"]).config([
      "$stateProvider",
      "$urlRouterProvider",
      function ($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise("/item");
        $stateProvider
        .state("list", {
          url: "/item",
          template: '
    {{error}}
    {{listvm}}
    ' + 'go to child state and trigger resolve' + '', controller: ["$scope", "$state", function($scope, $state){ $scope.listvm = { state: $state.current.name }; }] }) .state("list.detail", { url: "/{id}", template: '
    {{detailvm}}
    ', resolve: { data: ["$q", "$timeout","$http", function ($q, $timeout, $http) { return $http.get("/").then(function(){ return {status:true , data: "Yes"} }, function(){ return {status:false} }) }] }, controller: ["$scope", "data", "$state", function ($scope, data, $state) { $scope.detailvm = { state: $state.current.name, data: data.status ? data :"OOPS Error" }; }] }); } ]);
     
        
      

提交回复
热议问题