1

我正在尝试在由 node.js 服务器使用 express 支持的 AngularJS 应用程序中设置一些带有参数的路由。节点设置为将未明确找到的所有内容路由到所有内容:

app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.use(function(request, response)
{
    console.log("catch all");
    writeFile("/public/index.htm", request, response);
});

Angular 的路由定义了 0-2 个参数:

$routeProvider
.when("/",
{
    templateUrl: "home.htm"
})
.when("/otherpage",
{
    templateUrl: "otherpage.htm"
})  
.when("/otherpage/:Var1",
{
    templateUrl: "otherpage.htm"
})  
.when("/otherpage/:Var1/:Var2",
{
    templateUrl: "otherpage.htm"
})
.otherwise(
{
    templateUrl: "home.htm"
});

$locationProvider.html5Mode(true);

/otherpage 有效,但是当 /otherpage/123 全部命中(我可以看到控制台消息)时,它在浏览器中崩溃。什么都没有呈现,然后 Chrome 说它有问题(而 IE 没有呈现任何内容)。

我可以理解未传递的参数,但为什么没有它至少不会发生路由?使用 express.js 和 AngularJS 设置 URL 参数需要做什么?

如果您想检查它,您可以在此处下载该项目。

提前致谢。

4

1 回答 1

2

问题是我没有在我的 templateUrls 前面加上前斜杠。将它们更改为:

.when("/otherpage/:Var1/:Var2",
{
    templateUrl: "/otherpage.htm"
})

工作完美。

于 2013-04-04T12:47:52.603 回答