使用 UI 路由器处理查询字符串参数的动态集



假设我有一个 url,它有一个分解对象作为其查询字符串参数:"domain.com/example?id=1234&params[a]=hello&params[b]=world&params[foo]=bar//etc etc etc

">

如何配置角度 ui 路由器以接受这组动态查询字符串参数?

.state('example', {
    url: '/example?id&params[*]' ??
    ...
});

在这里,您的params似乎是一个关联数组。

您可以在此链接上看到 https://github.com/angular-ui/ui-router/issues/983克里斯托弗蒂伦的评论

.state('foo', { 
  url: '/foo/:param1?param2',
  params: { param3: null } // null is the default value
});
$state.go('foo', { param1: 'bar', param2: 'baz', param3: { id: 35, name: 'what' } });
$stateParams in 'foo' is now { param1: 'bar', param2: 'baz', param3: { id: 35, name: 'what' } }

并且根据这个答案 https://stackoverflow.com/a/29792020/1907391

在 0.2.13 版本中,您应该能够将对象传递到 $state.go 中,

$state.go('myState', {myParam: {some: 'thing'}})
$stateProvider.state('myState', {
                url: '/myState/{myParam:json}',
                params: {myParam: null}, ...
and then access the parameter in your controller.
$stateParams.myParam;

最新更新