好吧,这可能是 UI 路由器错误,也可能只是我的错。我在 Git 上发布了一个问题 https://github.com/angular-ui/ui-router/issues/2342 ,尚未回复,非常感谢如果有人可以在这里提供帮助
如果我做类似的事情:
angular.module('myapp', [ 'ui.router' ])
.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise("/state1");
$urlRouterProvider.rule(function ($injector, $location) {
return "/state2";
});
$stateProvider
.state('state1', {
url: "/state1",
views: {
"mainview": { template: "state1.html" }
}
})
.state('state2', {
url: "/state2",
views: {
"mainview": { template: "state2.html" }
}
});
})
.controller('AppCtrl', AppCtrl);
function AppCtrl(){
}
地址栏已更改,但 state2.html
未加载,只有一个空白页面,没有抛出任何错误
非常感谢您提供任何可能的解决方案。
最佳答案
将 .rule
视为 AOP 过滤器。这只是一个方面,
- 始终执行,当网址更改时,
- 并且可以调整该网址。
与其他情况相比,它被视为“最后一个 url 解析处理程序”。
因此,正如文档中所述,最好的用途是引入不区分大小写的 url 支持:
rule(rule)
Defines rules that are used by $urlRouterProvider to find matches for specific URLs.
rule
object
Handler function that takes $injector and $location services as arguments. You can use them to return a valid path as a string.example:
var app = angular.module('app', ['ui.router.router']); app.config(function ($urlRouterProvider) { // Here's an example of how you might allow case insensitive urls $urlRouterProvider.rule(function ($injector, $location) { var path = $location.path(), normalized = path.toLowerCase(); if (path !== normalized) { return normalized; } }); });
关于javascript - 如何使用 $urlRouterProvider.rule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404265/