本文翻译自:How to pass parameters using ui-sref in ui-router to controller
I need to pass and recieve two parameters to the state I want to transit to using ui-sref
of ui-router. 我需要使用ui-sref
的ui- ui-sref
将两个参数传递给我想转移到的状态。
Something like using the link below for transitioning the state to home
with foo
and bar
parameters: 类似于使用以下链接将状态转换为带有foo
和bar
参数的home
:
<a ui-sref="home({foo: 'fooVal', bar: 'barVal'})">Go to home state with foo and bar parameters </a>
Receiving foo
and bar
values in a controller: 在控制器中接收foo
和bar
值:
app.controller('SomeController', function($scope, $stateParam) {
//..
var foo = $stateParam.foo; //getting fooVal
var bar = $stateParam.bar; //getting barVal
//..
});
I get undefined
for $stateParam
in the controller. 我在控制器中为$stateParam
undefined
的内容。
Could somebody help me understand how to get it done? 有人可以帮我理解如何完成它吗?
Edit: 编辑:
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'home.html',
controller: 'MainRootCtrl'
},
'A@home': {
templateUrl: 'a.html',
controller: 'MainCtrl'
},
'B@home': {
templateUrl: 'b.html',
controller: 'SomeController'
}
}
});
#1楼
参考:https://stackoom.com/question/1jc4c/如何使用ui-router中的ui-sref将参数传递给控制器
#2楼
I've created an example to show how to. 我已经创建了一个示例来展示如何。 Updated state
definition would be: 更新的state
定义将是:
$stateProvider
.state('home', {
url: '/:foo?bar',
views: {
'': {
templateUrl: 'tpl.home.html',
controller: 'MainRootCtrl'
},
...
}
And this would be the controller: 这将是控制器:
.controller('MainRootCtrl', function($scope, $state, $stateParams) {
//..
var foo = $stateParams.foo; //getting fooVal
var bar = $stateParams.bar; //getting barVal
//..
$scope.state = $state.current
$scope.params = $stateParams;
})
What we can see is that the state home now has url defined as: 我们可以看到,州家现在已将网址定义为:
url: '/:foo?bar',
which means, that the params in url are expected as 这意味着,网址中的参数预计为
/fooVal?bar=barValue
These two links will correctly pass arguments into the controller: 这两个链接将正确地将参数传递到控制器:
<a ui-sref="home({foo: 'fooVal1', bar: 'barVal1'})">
<a ui-sref="home({foo: 'fooVal2', bar: 'barVal2'})">
Also, the controller does consume $stateParams
instead of $stateParam
. 此外,控制器确实使用$stateParams
而不是$stateParam
。
Link to doc: 链接到doc:
You can check it here 你可以在这里查看
params : {}
There is also new , more granular setting params : {}
. 还有新的 ,更精细的设置params : {}
。 As we've already seen, we can declare parameters as part of url
. 正如我们已经看到的,我们可以将参数声明为url
一部分。 But with params : {}
configuration - we can extend this definition or even introduce paramters which are not part of the url: 但是使用params : {}
配置 - 我们可以扩展这个定义,甚至引入不属于url的参数:
.state('other', {
url: '/other/:foo?bar',
params: {
// here we define default value for foo
// we also set squash to false, to force injecting
// even the default value into url
foo: {
value: 'defaultValue',
squash: false,
},
// this parameter is now array
// we can pass more items, and expect them as []
bar : {
array : true,
},
// this param is not part of url
// it could be passed with $state.go or ui-sref
hiddenParam: 'YES',
},
...
Settings available for params are described in the documentation of the $stateProvider 可用于params的设置在$ stateProvider的文档中描述
Below is just an extract 以下只是摘录
- value - {object|function=} : specifies the default value for this parameter. value - {object | function =} :指定此参数的默认值。 This implicitly sets this parameter as optional... 这隐式地将此参数设置为可选...
- array - {boolean=}: (default: false) If true, the param value will be treated as an array of values. array - {boolean =} :( default:false)如果为true,则param值将被视为值数组。
- squash - {bool|string=}: squash configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value. squash - {bool | string =}: squash配置当前参数值与默认值相同时,URL中如何表示默认参数值。
We can call these params this way: 我们可以这样称呼这些参数:
// hidden param cannot be passed via url
<a href="#/other/fooVal?bar=1&bar=2">
// default foo is skipped
<a ui-sref="other({bar: [4,5]})">
Check it in action here 在这里检查它
#3楼
You don't necessarily need to have the parameters inside the URL. 您不一定需要在URL中包含参数。
For instance, with: 例如,用:
$stateProvider
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'home.html',
controller: 'MainRootCtrl'
},
},
params: {
foo: null,
bar: null
}
})
You will be able to send parameters to the state, using either: 您可以使用以下任一方法将参数发送到州:
$state.go('home', {foo: true, bar: 1});
// or
<a ui-sref="home({foo: true, bar: 1})">Go!</a>
Of course, if you reload the page once on the home
state, you will loose the state parameters, as they are not stored anywhere. 当然,如果您在home
状态下重新加载一次页面,则会丢失状态参数,因为它们不会存储在任何位置。
A full description of this behavior is documented here , under the params
row in the state(name, stateConfig)
section. 此处记录了此行为的完整描述,位于state(name, stateConfig)
部分的params
行下。
#4楼
You simply misspelled $stateParam
, it should be $stateParams
(with an s). 你只是拼错了$stateParam
,它应该是$stateParams
(带有s)。 That's why you get undefined ;) 这就是你未定义的原因;)