javascript - AngularJS相对url路由问题

标签 javascript angularjs url-routing angular-routing

我的后端 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/

相关文章:

javascript - Angular ui 路由器。子状态参数基于父状态参数

angularjs - 如何在浏览器端解密在nodejs加密库上加密的数据?

node.js - Express.js - req.query 与 req.params

javascript - Node.js - listener must be a function 错误

javascript - TypeError : jQuery(. ..).find(...).carousel 不是函数

javascript - Angular JS 循环 promise

javascript - npm 安装 : install Couldn't read dependencies

grails - Grails URL映射优先规则

angular - 刷新页面时出现错误信息 "The requested URL was not found on this server."

Javascript 原型(prototype)常量声明