我有一个这样的状态定义:
$stateProvider.state('password', {
url: '/password',
templateUrl: 'password.html',
controller: 'PasswordController',
controllerAs: 'ctrl',
params: {
accountNumber: {
value: null,
squash: true
}
},
});
我需要使用其 href
引用此状态,而不是使用 ui-serif
指令,如何在 href 内设置 params.accountNumber
?
这不起作用:
$state.href('password', {accountNumber: $scope.accountNumber})
当我将此行 url: '/password',
更改为 ` url: '/password?accoutNumber' 并删除这部分
params: {
accountNumber: {
value: null,
squash: true
}
},
$state.href
工作得很好。
最佳答案
能够使用 $state.href
生成 URL 并传递参数...
- 此类参数必须是
url
定义的一部分 - 仅在
params: {}
中声明它会导致 href - 不包含它,不传递它
调整了状态定义:
.state('password', {
url: '/password/:accountNumber',
templateUrl: 'password.html',
controller: 'PasswordController',
controllerAs: 'ctrl',
params: {
accountNumber: {
value: null,
squash: true
},
},
});
原始部分,与拼写错误相关
$state.href()
调用似乎没问题...只是状态定义可能是错误的:
$stateProvider.state('password', {
//Url: '/password',
url: '/password',
...
url
设置区分大小写。请参阅state.href doc here :
href(stateOrName, params, options)
关于angularjs - 带有 $state.href 的 ui-sref params 选项未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201948/