AngularJS:无法使路由不区分大小写

时间:2016-03-16 23:34:05

标签: angularjs angular-routing

我无法使Angular(1.5)路由不区分大小写。我已经尝试将caseInsensitiveMatch: true添加到路由配置中,但除非我使用正确的案例,否则我的应用仍然无法加载。我想让路线不区分大小写。这是我的路线配置的样子:

module SoftwareRegistration {
    export class Routes {
        static $inject = ["$routeProvider", "$locationProvider"];
        static configureRoutes($routeProvider: ng.route.IRouteProvider, $locationProvider: ng.ILocationProvider) {
            $locationProvider.html5Mode({
                enabled: true,
                requireBase: true
            });

            $routeProvider.caseInsensitiveMatch = true;
            $routeProvider
                .when("/",
                {
                    controller: "SoftwareRegistration.ListOrdersController",
                    templateUrl: "app/views/order/listOrders.html",
                    controllerAs: "vm",
                    caseInsensitiveMatch: true
                })
                .when("/Orders",
                {
                    controller: "SoftwareRegistration.ListOrdersController",
                    templateUrl: "app/views/order/listOrders.html",
                    controllerAs: "vm",
                    caseInsensitiveMatch: true
                })
                .when("/Orders/MyOrders",
                {
                    controller: "SoftwareRegistration.ListOrdersController",
                    templateUrl: "app/views/order/listOrders.html",
                    controllerAs: "vm",
                    caseInsensitiveMatch: true
                })
                .when("/Orders/Create",
                {
                    controller: "SoftwareRegistration.CreateOrderController",
                    templateUrl: "app/views/order/createOrder.html",
                    controllerAs: "vm",
                    caseInsensitiveMatch: true
                })
                .when("/Orders/PaymentInformation",
                {
                    templateUrl: "app/views/order/paymentInformation.html",
                    caseInsensitiveMatch: true
                })
                .when("/Orders/:OrderId/AddRecipients",
                {
                    controller: "SoftwareRegistration.AddRecipientsController",
                    templateUrl: "app/views/order/addRecipients.html",
                    controllerAs: "vm",
                    caseInsensitiveMatch: true
                })
                .when("/Account/Login",
                {
                    controller: "SoftwareRegistration.LoginController",
                    templateUrl: "app/views/account/login.html",
                    controllerAs: "vm",
                    caseInsensitiveMatch: true
                })
                .otherwise({ redirectTo: "/", caseInsensitiveMatch: true });
        }
    }
}

当我导航到https://myserver.com/SoftwareRegistration时,它运行正常。但是,如果我导航到https://myserver.com/softwareregistration,我会收到此错误:

angular.js:12165 Uncaught TypeError: Cannot read property 'replace' of undefined

这是angular.js的来源:12165:

function trimEmptyHash(url) {
  return url.replace(/(#.+)|#$/, '$1');
}

我应该注意,它在我的本地工作正常(不区分大小写)但在部署到IIS时不起作用

1 个答案:

答案 0 :(得分:2)

我得到了一个解决方法,我对此问题感到满意:https://github.com/angular/angular.js/issues/3703

我在加载角度之前放置了这个小脚本,以动态地将<base>更改为用户在URL中键入的任何情况

(function() {
    var base = document.querySelector( "base" );
    var normalized = RegExp( base.href, "i" ).exec( location.href );
    base.href = normalized ? normalized[ 0 ] : base.href;
}());

这可能并不适用于所有情况。正如https://github.com/angular/angular.js/issues/4056中提到的,这可能会打破&#34;缓存。但是,我最喜欢这个解决方案,因为我不需要修改源代码(或者每次更新时都记得更改它),也不必乱用任何服务器设置(无论如何都在IIS上)。

相关问题