2016-03-20 22 views
0

以下是我的路由配置角路由解析 - deferred.reject不工作 - 角1 +打字稿

function routes($routeProvider: ng.route.IRouteProvider) { 

     let accessResolver = ['UserFactory', (UserFactory: any) => { 
      return UserFactory.isAuthenticated(); 
     }]; 

     //Configuring routes 
     $routeProvider.when('/', { 
      templateUrl: '/views/home.html', 
      controller: 'HomeController', 
      controllerAs: 'homeCtrl', 
      resolve: accessResolver 
     }).when('/login', { 
      templateUrl: '/views/login.html', 
      controller: 'LoginController', 
      controllerAs: 'loginCtrl' 
     }).otherwise({ 
      redirectTo: '/' 
     }); 
    } 

我的路線更改錯誤處理程序

function run($rootScope: ng.IRootScopeService, $location: ng.ILocationService) { 
     $rootScope.$on("$routeChangeError",() => { 
      console.log("Route Change Error"); 
      $location.url('/login?redirect=' + $location.url()); 
     }); 
    } 

而且UserFactory

module TheHub { 
    export interface IUserFactory { 
     isAuthenticated(): ng.IDeferred<String>; 
    } 

    class UserFactory implements IUserFactory { 

     constructor(private $http: ng.IHttpService, private $q: ng.IQService, private $rootScope: any) { 
     } 

     isAuthenticated(): ng.IDeferred<String> { 
      let deferred = this.$q.defer(); 
      if (this.$rootScope.auth && this.$rootScope.auth.isAuthenticationChecked) { 
       if (this.$rootScope.auth.isAuthenticated) { 
        deferred.resolve('OK'); 
       } else { 
        deferred.reject('Unauthorized'); 
       } 
      } else { 
       this.$http.get('secure/user').then(
        (response: ng.IHttpPromiseCallbackArg<{}>) => { 
         if (!this.$rootScope.auth) { 
          this.$rootScope.auth = {}; 
         } 
         this.$rootScope.auth.isAuthenticationChecked = true; 
         this.$rootScope.auth.isAuthenticated = true; 
         deferred.resolve('OK'); 
        }, 
        (error: any) => { 
         if (!this.$rootScope.auth) { 
          this.$rootScope.auth = {}; 
         } 
         this.$rootScope.auth.isAuthenticationChecked = true; 
         deferred.reject('Unauthorized'); 
        }); 
      } 
      return deferred; 
     } 
    } 

    function userFactory($http: ng.IHttpService, $q: ng.IQService, $rootScope: any) { 
     return new UserFactory($http, $q, $rootScope); 
    } 

    userFactory.$inject = ['$http', '$q', '$rootScope']; 

    angular.module('TheHub').factory('UserFactory', userFactory); 
} 

這裏的邏輯是,我發射一個請求來檢查用戶是否已經登錄並且有一個會話。 問題是,當用戶尚未登錄時,服務失敗,承諾被拒絕。但是,我不知道爲什麼,處理程序$ routeChangeError沒有被解僱。當出現JavaScript錯誤時它工作正常。

+2

你需要使用'返回deferred.promise',但你真的應該徹底[避免遞延反模式](http://stackoverflow.com/ q/23803743/1048572) – Bergi

+0

非常感謝..這是一個愚蠢的錯誤..你可以發佈這個答案,以便我可以標記它解決.. – Pavan

回答

1

您忘記了.promise,以便您只返回延期的,既沒有等待也沒有您期望的分辨率值。

但是你應該avoid the deferred antipattern anyway - 只是做

isAuthenticated(): ng.IPromise<String> { 
    if (this.$rootScope.auth && this.$rootScope.auth.isAuthenticationChecked) { 
     if (this.$rootScope.auth.isAuthenticated) { 
      return this.$q.resolve('OK'); 
//   ^^^^^^^^^^^^^^^^^^^^^^ 
     } else { 
      return this.$q.reject('Unauthorized'); 
//   ^^^^^^^^^^^^^^^^^^^^^^ 
     } 
    } else { 
     return this.$http.get('secure/user').then(
//  ^^^^^^ 
      (response: ng.IHttpPromiseCallbackArg<{}>) => { 
       if (!this.$rootScope.auth) { 
        this.$rootScope.auth = {}; 
       } 
       this.$rootScope.auth.isAuthenticationChecked = true; 
       this.$rootScope.auth.isAuthenticated = true; 
       return 'OK'; 
//    ^^^^^^ 
      }, 
      (error: any) => { 
       if (!this.$rootScope.auth) { 
        this.$rootScope.auth = {}; 
       } 
       this.$rootScope.auth.isAuthenticationChecked = true; 
       return this.$q.reject('Unauthorized'); 
//    ^^^^^^^^^^^^^^^^^^^^^ 
      } 
     ); 
    } 
} 
+1

我想'ng.IDeferred '應該也是固定的。 – estus

+0

@estus:哦,對,我忘了它是打字稿。你知道'ng.IPromise '是否是正確的類型嗎? – Bergi

+1

我不會經常打字,但是,我確信這是正確的打字方式。另外,'throw'與$ q中的$ q.reject()不能互換。前者還會觸發異常處理程序,我會爲可能未處理的應用程序錯誤保存'throw'。 – estus