gpt4 book ai didi

angularjs - 如何访问状态参数(ui-sref)而不将它们包含在AngularJS中状态的URL属性中

转载 作者:行者123 更新时间:2023-12-03 06:52:25 24 4
gpt4 key购买 nike

This question is might be similar to this but with different requirements 。由于我无法发表评论(需要 50 分),所以我正在复制这个问题。

我想简单地访问 Controller 内模板中从 ui-sref 发送的参数,而不在状态 URL 中提及它们。

类似于使用下面的链接通过 foo 和 bar 参数将状态转换为 home :

<a ui-sref="home({foo: 'fooVal', bar: 'barVal'})">Go to home state with foo and bar parameters </a>

在 Controller 中接收 foo 和 bar 值:

state('home', {
url: '/:foo',
views: {
'***whatIsThis***': {
templateUrl: 'home.html',
controller: 'MainRootCtrl'
},

app.controller('SomeController', function($scope, $stateParam) {
//..
var foo = $stateParam.foo; //getting fooVal
var bar = $stateParam.bar; //getting barVal
//..
});

我在 Controller 中未定义 $stateParam。

有人可以帮助我了解如何完成它吗?我也想获得 bar 而不将其添加到 URL

最佳答案

如果您的某些参数不应显示在 URL 中,则需要组合 urlparams:

.state('home', {
url: '/:foo',
params: {
foo: 'default value of foo',
bar: 'default value of bar'
},
...
})

然后正确使用$stateParams:

.controller('SomeController', ['$scope', '$stateParams', function ($scope, $stateParams) {
// ... (use $stateParams)
}])

(see documentation)。

<小时/>

如果您仍然遇到困难,请看看这个 working codepen demo .

关于angularjs - 如何访问状态参数(ui-sref)而不将它们包含在AngularJS中状态的URL属性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27499085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com