【发布时间】:2015-02-05 18:14:57
【问题描述】:
我在 Angular.js 和 MVC5 中的路由遇到了一些问题。我已将其简化为以下示例。
我的角度路由代码是:
app.config(["$routeProvider", "$locationProvider", function ($routeProvider, $locationProvider) {
$routeProvider.when("/", {
template: "<h1>index</h1>",
})
.when("/Home/About", {
template: "<h1>about</h1>",
})
.when("/Home/Contact", {
template: "<h1>contact</h1>",
})
.otherwise({
template: "<h1>Error</h1>",
});
}]);
MVC Home 控制器对每个控制器都有一个方法,每个视图都有一个带有 ng-view 属性的 DIV。
当我浏览到 about 或 contact 时,路由仍然映射到 Index。
如果我将索引路径更改为:
$routeProvider.when("/Home/Index", {
template: "<h1>index</h1>",
})
然后否则启动,我看到了
错误
。代码看起来与我使用的其他角度代码以及网络上的示例相同。有什么建议吗?
更新:感谢以下答案。我想我昨晚没有很好地解释我的问题,这是漫长的一天的结果。我的问题是我试图在网站的子页面上放置一个迷你水疗中心,所以主页的路线是:
.when("/userPermissions/index", {
templateUrl: "/scripts/bookApp/userPermissions/main.html",
controller: "userPermissionController",
})
并且“/userPermissions/index”的路径将是服务器提供的页面,没有被路由匹配。
【问题讨论】:
-
你使用的是html5模式吗?
-
您必须初始化控制器 - 例如控制器:“HomeController”,模板:...
-
不,不使用 html5 模式。在简化版本中,我没有使用角度控制器,只是替换模板。
-
正如下面的答案中提到的,我试图在两个单独的“服务器”页面上拥有一个角度 SPA 时让自己感到困惑。我将我的 SPA 分成两个单独的模块,并将它们分别托管在两个“服务器”页面上,一切都很好。
标签: asp.net-mvc angularjs angular-routing