2015-06-16 57 views
4

是否有可能只允許使用angularjs在routeProvider中進行某些選擇?Angular routeProvider聲明參數集?

舉例來說,如果我有:

$routeProvider.when('/:action/view/', { 

我可以只允許(「籃球」,「棒球」)作爲此動作被設置爲匹配?

回答

0

您可以使用redirectTo屬性路線的重定向用戶不允許路線呼叫的情況下:

$routeProvider.when('/:action/view/',{ 
    redirectTo: function(routeParams, locationPath, locationSearch){ 
     var allowed = ['baseball','basketball'];  //allowed action param 

     if(allowed.indexOf(routeParams.action) != -1){ //route is allowed don't redirect 
      return null; 
     } 
     else{           //route is denied redirect to home 
      return '/home'; 
     } 
    } 
} 
0

沒有測試,但你可以聽上$ routeChangeStart,這之前調用路由變化,使用「下一個」和「當前」路線參數來確定是否允許未來路線。如果不是,則執行$ location.path將其設置回當前路由本身。