2017-08-29 6 views
0

角度アプリでリダイレクトを設定したいと思います。ui-router/Angular JSでリダイレクトを設定する

は、今私が持っている:

$urlRouterProvider.otherwise('/notfound'); 

パスが定義された状態のいずれにも一致doesntの場合はそう、それは「見つからない」状態になります。

ただし、パスが定義された状態のいずれとも一致しない場合、最初にリダイレクトの配列に対する角度チェックが行われるようにこの動作を変更したいと思います。一致しない場合、「見つからない」になります。一致するものがある場合、angleは一致するURLへのハードリダイレクトを行います。擬似コードで

:パスがすでにあるので

var redirects = [{'/a_certain_path':'http://www.someaddress.com/'}]; 

if(URL != defined state){ // path doesnt match any state 

    if(URL in redirects araay){ // path matches value in array 

     window.location = redirects[URL]; // redirect to match 

    } else $state.go('base.notfound'); 

} else { 

    $state.go('base.notfound'); 
} 

アイブ氏は明らかにこのdoesntの仕事を(私の「が見つかりません」状態に決意を使用してのような)いくつかのことを試してみましたが、「/ NOTFOUND」時間によってIそこに着いてください。

+0

の可能性のある重複した[どのように私は、UI-ルータは外部リンクに行くだろうに?](https://stackoverflow.com/a/30221248/2435473) –

+0

ない重複これは、必要な特定のロジックが含まれているとして、 URLをリダイレクトと照合する – yevg

答えて

1
var redirects = [{name:'/a_certain_path',url:'http://www.someaddress.com/'}]; 

    $urlRouterProvider.otherwise(function ($injector) { 
      var $state = $injector.get('$state'); 
      if(redirects){ 
        var firstRedirect=redirects[0]; 
        redirects.splice(0,1);//if there is more than one 
               redirects not check invalid 
               again 
        if($state.href(firstRedirect.name)) 
        { 
           $state.go(firstRedirect.name) 
        } 
        else 
        { 
          window.location =firstRedirect.url; 
        } 
      } 
      else{ 
       $state.go('base.notfound'); 

      } 

     });