对于查询参数,您应该使用$state.params
代替$stateParams
状态配置:
stateProvider.state({
name: 'search',
url: '/search?q',
//...
}
控制器来自:
function fromCtrl ($state) {
$state.go('search', {q: vm.searchTerms});
}
或模板/HTML 链接来自:
<a ui-sref="search({q:'abc'})">my Link</a>
控制器至:
function toCtrl ($state) {
this.searchTerms = $state.params.q;
}
UPDATE: use
$transition$
for new versions >= 1.0.0 (
PLUNKER DEMO https://plnkr.co/edit/ZFGntE4FORWPEzZQaoZH?p=preview)
The code above is the same for both versions, only you need to change the
toCtrl
...
function toCtrl ($transition$) {
this.myParam = $transition$.params().q;
}
如果您的 searchTerms 是一个对象,您可以使用JSON.stringify()
and JSON.parse()
如果您仍然有任何疑问,请查看这些帖子:
如何使用 AngularJS 的 ui-router 提取查询参数? https://stackoverflow.com/questions/19053991/how-to-extract-query-parameters-with-ui-router-for-angularjs
AngularJS:使用 ui-router 将对象传递到状态 https://stackoverflow.com/questions/20632255/angularjs-pass-an-object-into-a-state-using-ui-router