我的后端 Web 框架使用以下 URL 加载我的 AngularJS 应用程序
http://localhost/New/Alpha/App
我还设置了它,以便 App
之后的任何内容仍然会加载相同的东西
http://localhost/New/Alpha/App/home
http://localhost/New/Alpha/App/settings
...
我正在尝试让我的 AngularJS 应用程序以在 App
之后获取 URL 的方式工作。并相应地加载 Controller /模板。不过,我的 AngularJS 应用程序中的路由有问题
var main = angular.module("main", ["ui.bootstrap", "ngRoute"]);
main.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("home", {
templateUrl: "assets/tpl/home.html",
controller: "mainController"
})
.otherwise({
redirectTo: "fail"
});
$locationProvider.html5Mode(true);
});
main.controller("mainController", function($scope) {
console.log("home")
});
如果我尝试这个网址
http://localhost/New/Alpha/App/home
它将 URL 更改为
http://localhost/fail
而不是保持 URL 不变并加载模板/ Controller 。但是,如果我更改配置并为其提供完整的相对 URL,它就会按预期工作
.when("/New/Alpha/App/home", {
templateUrl: "assets/tpl/home.html",
controller: "mainController"
})
我的问题是,App
之前的 URL 部分-/New/Alpha
无法硬编码。它可能是 /New/Beta
, /New/Gamma
等
我想要做的事情是否可以在不硬编码完整相对 URL 匹配的情况下实现?
更新抱歉,忘记提及 App
之前的网址段数可以改变,就像 /New/Beta/App
也可以是/New/Another/Beta/App
。我不认为像 */App
这样的东西或/New/*/App
可以代替 /New/:placeholder/App
?
最佳答案
这对你有用吗?
var main = angular.module("main", ["ui.bootstrap", "ngRoute"]);
main.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/New/:greek/App/home", {
templateUrl: "assets/tpl/home.html",
controller: "mainController"
})
.otherwise({
redirectTo: "fail"
});
$locationProvider.html5Mode(true);
});
main.controller("mainController", function($scope) {
console.log("home")
});
然后,您可以从 Controller 中使用 $routeParams.greek
检索希腊语。
关于javascript - AngularJS相对url路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113361/