AngularJS: 无法使路由不区分大小写
AngularJS: Unable to make routes case insensitive
我无法使 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 it works fine. However, if I navigate to https://myserver.com/softwareregistration 时出现此错误:
angular.js:12165 Uncaught TypeError: Cannot read property 'replace' of undefined
这里是 angular.js:12165 的来源:
function trimEmptyHash(url) {
return url.replace(/(#.+)|#$/, '');
}
我应该注意,它在我的本地(不区分大小写)上运行良好,但在我部署到 IIS 时不起作用
我从这个问题得到了一个令我满意的解决方法:https://github.com/angular/angular.js/issues/3703
我在加载 angular 之前放置了这个小脚本,以动态地将 <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 中所述,这可能 "break" 缓存。但是,我最喜欢这个解决方案,因为我不必 mod 源(或者记得每次更新时都更改它),也不必弄乱任何服务器设置(无论如何在 IIS 上)。
我无法使 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 it works fine. However, if I navigate to https://myserver.com/softwareregistration 时出现此错误:
angular.js:12165 Uncaught TypeError: Cannot read property 'replace' of undefined
这里是 angular.js:12165 的来源:
function trimEmptyHash(url) {
return url.replace(/(#.+)|#$/, '');
}
我应该注意,它在我的本地(不区分大小写)上运行良好,但在我部署到 IIS 时不起作用
我从这个问题得到了一个令我满意的解决方法:https://github.com/angular/angular.js/issues/3703
我在加载 angular 之前放置了这个小脚本,以动态地将 <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 中所述,这可能 "break" 缓存。但是,我最喜欢这个解决方案,因为我不必 mod 源(或者记得每次更新时都更改它),也不必弄乱任何服务器设置(无论如何在 IIS 上)。