2013-07-28 23 views
1

我大致知道在AngularJS路由定義發生如下:分佈式路由定義

var app = angular.module('app', []); 
app.config(function ($routeProvider) { 
$routeProvider.when("/", { templateUrl: "/partials/home.html" }). 
       when("/profile", { templateUrl: "/partials/profile.html" }). 
       when("/contact", { templateUrl: "/partials/contact.html" }). 
       otherwise({ redirectTo: '/' }); 
}); 

什麼惱火的是,我想用RequireJS模塊化我的申請,我想註冊需要的地方的路線。例如,如果我有一個配置文件模塊,我想從那裏註冊我的路線。同樣適用於聯繫人模塊。這種集中化讓我瘋狂。我在這裏錯過了一個觀點,還是有什麼好的解決方案來解決我的問題?

+0

@Stewie你是對的 - 我沒有找到這個 –

回答

1

您可以簡單地在模塊上分配路由配置。你必須做的唯一事情就是在你的「應用程序」模塊中註冊所有的模塊。

angular.module('app', ['app.profile', 'app.contact']) 
    .config(function ($routeProvider) { 
    $routeProvider.when("/", { templateUrl: "/partials/home.html" }) 
        .otherwise({ redirectTo: '/' }); 
    }); 

angular.module('app.profile', []) 
    .config(function ($routeProvider) { 
    $routeProvider.when("/profile", { templateUrl: "/partials/profile.html" }); 
    }); 

angular.module('app.contact', []) 
    .config(function ($routeProvider) { 
    $routeProvider.when("/contact", { templateUrl: "/partials/contact.html" }); 
    }); 
+0

這只是有道理 - 謝謝! –

1

您不必在同一個地方完成$ routeProvider的配置。根據需要添加路由定義,即當您知道您將使用例如配置文件模塊時,可以添加與其路由對應的.when()子句。