2014-09-25 41 views
0

使用Angular我有十幾個路由設置類似於以下示例代碼。

有沒有一種方法來覆蓋哪些模板和控制器是基於其他標準加載的,同時保持URL的機智?我的目標是顯示一個登錄頁面...讓我們說$ scope.isLoggedIn = false。我不想將URL更改爲/ login。

SomeApp.config(['$routeProvider', function($routeProvider) { 
    $routeProvider. 
    when('/place', { 
     templateUrl: 'routes/place.html', 
     controller: 'PlaceCtrl' 
    }) 
    .when('/test', { 
     templateUrl: 'routes/test.html', 
     controller: 'TestCtrl' 
    }); 
}]); 

回答

1

ngRoute是一個非常簡單的庫,它基本上只能將urls映射到controller/views。如果您想要更大的靈活性,請嘗試ui-router,它可以根據狀態進行路由。

1

這對ngRoute來說並不適用,但是使用ui-router,您可以根據您想要的任何內容動態提供不同的模板。

$stateProvider.state('root', 
    url: '/' 
    controller: 'HomePageController' 
    templateProvider: [ 
    '$rootScope' 
    '$templateCache' 
    '$http' 
    ($rootScope, $templateCache, $http) -> 
     templateId = if $rootScope.isLoggedIn then "home-page-logged-in" else "home-page-not-logged-in" 
     templateId = "/templates/#{templateId}.html" 

     return $http.get(templateId, cache: $templateCache) 

    ] 
) 

據瞭解,據我所知,您不能更改控制器,只能更改模板。這有點臭。

+0

雖然您可以始終使用ng-controller屬性將控制器放在模板中,並且完全將其從路由中排除。 :) – Stuart 2016-09-12 08:54:57