2017-06-09 15 views
1

私が達成しようとしているのは、セッションがどのページからタイムアウトした場合、ユーザーをログインページにリダイレクトしていて何らかのエラーを表示したいのです。今はリダイレクトできますが、エラー部分は機能していません。角度:エラーページと一緒にログインページにリダイレクト

これは私のhttpInterceptor

@app.service 'httpInterceptor', [ 
    '$window', 
    'AuthenticationService' 
    ($window, AuthenticationService) -> 
    { 
     responseError: (res) -> 
     if res.status == 403 
      AuthenticationService.error = true; 
      $window.location.href = '/' 
     res 
    } 
] 

と私は

@app.service 'AuthenticationService', -> 
    @error = false 
    return 

AuthenticationService.errorこのサービスを利用しようとしていますが、私はまだ、この値はfalseとして取得していますログインコントローラです。

EDIT JSコード

this.app.service('httpInterceptor', [ 
    '$window', 
    'AuthenticationService', 
    ($window, AuthenticationService) => 
    ({ 
     responseError(res) { 
     if (res.status === 403) { 
      AuthenticationService.error = true; 
      $window.location.href = '/'; 
     } 
     return res; 
     } 
    }) 

]); 

サービス

this.app.service('AuthenticationService', function() { 
    this.error = false; 
}); 
+0

これはどの言語ですか? coffeescript?角で:)? – PierreDuc

+0

はいそれはcoffeescriptです – Saad

+0

@PierreDuc私はJSコード – Saad

答えて

0

使用$ロケーションサービスビューを変更するためのstateProviderを使用している場合は、locationRouterまたは使用$状態サービスを使用している場合。

例 - $ location.path( '/'); $ state.go( '/');

これにより、エラーメッセージ用に設定したサービスの値がリセットされなくなります。

this.app.service('httpInterceptor', [ 
    '$window', 
    'AuthenticationService','$location;, '$state', 
    ($window, AuthenticationService) => 
    ({ 
     responseError(res) { 
     if (res.status === 403) { 
      AuthenticationService.error = true; 
      //$window.location.href = '/'; // instead of this 
      // Use below 
      $state.go('/'); or $location.path('/'); 

     } 
     return res; 
     } 
    }) 

]); 
+0

私がそこで行ったようにサービスを注入することを忘れないでください。 –

+0

私は位置と状態の両方を試しましたが、場所は '#/'を変更せずに追加するだけで、1ページのアプリケーションではありません。 $状態でもエラーが発生しています。 – Saad

0

あなたが$window.location.href = '/';を行うと、ページを再読み込みする必要があり、そのサービスに設定したすべてがリフレッシュ後にクリアされます。ページを更新する必要がない場合は、$locationまたは$stateとお勧めします。まだページをリロードしたい場合は、ページ/状態間で情報をパスする必要があります。 localStorageを使用するか、url .href = '/?authError=Y'にクエリパラメータを追加して、アプリのブートストラップで読み取り+清掃してください。

更新:あなたのコメントに基づいて、角ページではない別のページにリダイレクトする必要がありますか?そうであれば、$locationでも$stateでも角度を知ることはできません($location.href = "\"を実行できますが、それは$window.location.href = '/';と同じです)。

P.S. cofeescript ...あなたは変態です: - P

関連する問題