繁体   English   中英

AngularJS一页具有多种状态

[英]AngularJS one page with multiple states

我正在使用angular-ui-router开发angularjs应用程序。 有一个页面列出了项目,可以按名称过滤,并且在URL中显示/ items?name = foo之类的内容 还有另一个页面(例如,用户),您可以从中导航到项目页面,这些页面由用户过滤,例如/ items?userid = bar

我通过在路由器配置中为同一页面设置两个状态来实现这一点:

.state('index.items', {
   url: '/items?:name',
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})
.state('index.itemsOfUser', {
   url: '/items?:userid&:name',
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})

我这样做是为了单击导航栏上的项目页面,然后转到普通项目页面,而不是用户过滤的页面。

但是,当从用户导航到项目页面时,userid出现在URL中,但在$ stateparams中,userid是未定义的,我不明白为什么。

我不知道我的方法是否正确,我是新手。

ui-router启用查询字符串参数的检索。 您的代码几乎是正确的,只是一个小的语法修复:

.state('index.items', {
   url: '/items?name', // Remove semicolon
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})
.state('index.itemsOfUser', {
   url: '/items?userid&name', // Remove semicolons
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})

如文档中所述: https : //github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM