【发布时间】:2014-06-26 00:12:19
【问题描述】:
自从我开始解决这个问题以来已经有几个小时了,我似乎无法理解解决方案。
我有一个应用程序可能会导致用户实际输入 URL。在这种情况下,不难相信用户可能会输入斜杠。例如,
www.example.com/users/2 和 www.example.com/edit/company/123
应该和
一样对待www.example.com/users/2/ 和 www.example.com/edit/company/123/
这只需要在客户端处理 URL 路由。我对处理资源/API 调用中的斜杠不感兴趣。我只对处理浏览器中的斜杠感兴趣。
所以我研究并在网上找到了很多答案。他们中的大多数将我带到了 angular-ui 路由器的常见问题解答部分。
https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions
他们告诉我们写一个规则,这是我想做的,但它似乎不起作用,或者我做错了。
这是我添加代码的 plunkr。
http://plnkr.co/edit/fD9q7L?p=preview
我已将此添加到我的配置中,其余代码几乎是基本内容。
$urlRouterProvider.rule(function($injector, $location) {
//if last charcter is a slash, return the same url without the slash
if($location.$$url[length-1] === '/') {
return $location.$$url.substr(0,$location.$$url.length - 2);
} else {
//if the last char is not a trailing slash, do nothing
return $location.$$url;
}
});
基本上,我想让尾部斜杠可选,即它在地址栏上的存在或不存在对加载的状态没有影响。
【问题讨论】:
标签: angularjs angular-ui angular-ui-router angular-routing