ski*_*kip 364 html javascript angularjs angular-ui angular-ui-router
我需要传递并接收两个参数到我想转移到使用ui-sref
ui-router的状态.
比如使用以下链接将状态转换为home
with foo
和bar
参数:
<a ui-sref="home({foo: 'fooVal', bar: 'barVal'})">Go to home state with foo and bar parameters </a>
Run Code Online (Sandbox Code Playgroud)
控制器中的接收foo
和bar
值:
app.controller('SomeController', function($scope, $stateParam) {
//..
var foo = $stateParam.foo; //getting fooVal
var bar = $stateParam.bar; //getting barVal
//..
});
Run Code Online (Sandbox Code Playgroud)
我在控制器中得到undefined
了$stateParam
.
有人可以帮我理解如何完成它吗?
编辑:
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'home.html',
controller: 'MainRootCtrl'
},
'A@home': {
templateUrl: 'a.html',
controller: 'MainCtrl'
},
'B@home': {
templateUrl: 'b.html',
controller: 'SomeController'
}
}
});
Run Code Online (Sandbox Code Playgroud)
Rad*_*ler 530
我已经创建了一个示例来说明如何.更新的state
定义是:
$stateProvider
.state('home', {
url: '/:foo?bar',
views: {
'': {
templateUrl: 'tpl.home.html',
controller: 'MainRootCtrl'
},
...
}
Run Code Online (Sandbox Code Playgroud)
这将是控制器:
.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;
})
Run Code Online (Sandbox Code Playgroud)
我们可以看到,州家现在已将网址定义为:
url: '/:foo?bar',
Run Code Online (Sandbox Code Playgroud)
这意味着,网址中的参数预计为
/fooVal?bar=barValue
Run Code Online (Sandbox Code Playgroud)
这两个链接将正确地将参数传递到控制器:
<a ui-sref="home({foo: 'fooVal1', bar: 'barVal1'})">
<a ui-sref="home({foo: 'fooVal2', bar: 'barVal2'})">
Run Code Online (Sandbox Code Playgroud)
此外,控制器确实消耗$stateParams
而不是$stateParam
.
链接到doc:
你可以在这里查看
params : {}
还有新的,更精细的设置params : {}
.正如我们已经看到的,我们可以将参数声明为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',
},
...
Run Code Online (Sandbox Code Playgroud)
可用于params的设置在$ stateProvider的文档中描述
以下只是摘录
我们可以这样称呼这些参数:
// 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]})">
Run Code Online (Sandbox Code Playgroud)
在这里检查它
Pau*_*gel 107
您不一定需要在URL中包含参数.
例如,用:
$stateProvider
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'home.html',
controller: 'MainRootCtrl'
},
},
params: {
foo: null,
bar: null
}
})
Run Code Online (Sandbox Code Playgroud)
您可以使用以下任一方法将参数发送到州:
$state.go('home', {foo: true, bar: 1});
// or
<a ui-sref="home({foo: true, bar: 1})">Go!</a>
Run Code Online (Sandbox Code Playgroud)
当然,如果您在home
状态上重新加载一次页面,您将丢失状态参数,因为它们不会存储在任何位置.
此处记录了此行为的完整描述,params
位于该state(name, stateConfig)
部分的行下.
fel*_*ker 29
你只是错误拼写$stateParam
,它应该是$stateParams
(带有s).这就是你未定义的原因;)
归档时间: |
|
查看次数: |
367335 次 |
最近记录: |