如何解决接受指定状态URL后的任何后续字符
我正在使用ui-router
,并且我的一个状态URL可以附加动态键。所以状态看起来像这样:
.state('reset-password',{
url: '/reset-password/{.*}',template: require('html-loader!./features/forgot-password/html/reset-password.tpl.htm'),controller: 'ResetPasswordController',controllerAs: '$ctrl',})
但是URL可能是/reset-password/134042?emailAddress=email@email.com
。
但是,如果输入了该状态,将不会识别该状态,并且默认情况下会通过加载/login
状态:
$urlRouterProvider
.otherwise("/login")
问题
在/reset-password
之后,如何允许任何后续的动态URL?
解决方法
如果您希望URL的尾部匹配状态,则可以使用以下(等效)语法之一:
-
/reset-password/{path:.*}
-
/reset-password/*path
要注意的共同点是结尾部分绑定到名称(即path
)。
然后,您应该可以通过控制器中的$stateParams
进行访问。
或者,如果您想让ui-router
做更多解析URL参数的繁重工作,您也可以这样做。
包括以下一些方法:
angular
.module('app',['ui.router'])
.config(function ($stateProvider) {
$stateProvider
.state({
name: 'reset-password1',url: '/reset-password1/*path',controller: function ($stateParams) {
this.path = $stateParams.path;
},controllerAs: '$ctrl',template: '<pre>path={{$ctrl.path}}</pre>'
})
.state({
name: 'reset-password2',url: '/reset-password2/:id?email',controller: function ($stateParams) {
this.id = $stateParams.id;
this.email = $stateParams.email;
},template: '<pre>id={{$ctrl.id}} email={{$ctrl.email}}</pre>'
});
});
<div ng-app="app">
<a ui-sref="reset-password1({ path: '134042?email=email@email.com' })" ui-sref-active="active">Reset Password (1)</a>
<a ui-sref="reset-password2({ id: 134042,email: 'email@email.com' })" ui-sref-active="active">Reset Password (2)</a>
<ui-view></ui-view>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.0/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.28/angular-ui-router.min.js "></script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。