我正在将我的应用程序从使用简单的Angular路由器服务转换为UI-Router。我有一个路由,其中有一些参数来自搜索字段。在Angular提供的路由器服务中,如果我们搜索空输入字段,由于其他功能,URL被重定向到主页,但在UI-Router中,它会转到此URL http://localhost:3000/#/resultPage//1。
我尝试使用带有参数的正则表达式,但它不起作用。我正在使用的代码在下面提到
$urlRouterProvider.otherwise('/');
.state("resultpage", {
url: "/resultPage/{searchParameter:[0-9a-zA-Z]+}/{pageParameter:[0-9]}",
templateUrl: "templates/resultpage.html",
controller: "resultController",
})
如果URL中省略了任何参数,我如何确保将URL重定向到主页?
答案 0 :(得分:0)
传递变量而不是直接值。
例如,url: "/resultPage/:paramOne/:paramTwo"
,此处paramOne
和paramTwo
是从ui-sref
传递的参数。
现在在控制器中添加$stateParams
到依赖注入以获取url参数。
app.controller('myCtrl', function($scope, $stateParams, $state){
if(!$stateParams.paramOne || !$stateParams.paramOne){
$state.go('', {}); // redirecting to '/'
}
})
现在,是时候设置您想要访问的州的名称了。
前:
<a ui-sref="resultPage( {paramOne: {searchParameter:[0-9a-zA-Z]+}, paramTwo: {pageParameter:[0-9]} })"
答案 1 :(得分:0)
您可能应该使用查询参数来代替此用例。请在此处查看文档的第一段:https://ui-router.github.io/docs/0.3.1/#/api/ui.router.util.type:UrlMatcher