[英]How to pass parameters using ui-sref in ui-router to the controller
I need to pass and receive two parameters to the state I want to transit to using ui-sref
of ui-router.我需要将两个参数传递和接收到我想要过渡到使用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.我在控制器中undefined
$stateParam
。
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'
}
}
});
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:我们可以看到,state home 现在的 url 定义为:
url: '/:foo?bar',
which means, that the params in url are expected as这意味着,url 中的参数应为
/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:链接到文档:
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 $stateProvider的文档中描述了可用于参数的设置
Below is just an extract以下只是摘录
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]})">
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
行下。
You simply misspelled $stateParam
, it should be $stateParams
(with an s).您只是拼错$stateParam
,它应该是$stateParams
(带有 s)。 That's why you get undefined ;)这就是为什么你得到未定义的;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.