问题描述
如果我有一个抽象状态:
if i have an abstract state:
$stateProvider.
state('drug', {
abstract: true,
url: '/drug/:drugId',
template:
'<ui-view></ui-view>',
resolve: {
drugId : ['$stateParams', function($stateParams){
return $stateParams.drugId;
}]
}
}
有一些子状态,形式为:drug.x,drug.y我想在应用程序的一个地方选择一个 DrugId,它会通过抽象状态到达所有子状态,这样之后当我调用drug.x"状态时,它就会有 drugId 值——我在哪里以及如何使用 drugId 参数调用药物状态?我知道我不能调用抽象状态本身.
that have some child states, in form: drug.x,drug.yand I want to choose, in one place in the app, a drugId that will come through the abstract state to all the child states, so that after that when i call to 'drug.x' state, it'll have the drugId value -where and how i do this one call to drug state with drugId param?I know that I can't call the abstract state itself.
谢谢.
推荐答案
通过将 $stateParams.drugId 作为解析公开,您已经完成了棘手的部分.现在你只需像这样将它注入你的子状态控制器:
You have already done the tricky part by exposing $stateParams.drugId as a resolve. Now you just inject it into your substate controller like so:
$stateProvider.state('drug.x', {
controller: function(drugId) { } // drugId is injected from the resolve you defined in 'drug'
}
要将参数提供给 drug.x
,您只需将其添加到转换参数:
To provide the parameter to drug.x
, you simply add it to the transition parameters:
$state.go('drug.x', { drugId: 123 })
;
或
<a ui-sref="drug.x({ drugId: scopeVariable })">转到 drug.x 获取 {{ scopeVariable }}</a>
这篇关于抽象状态、ui-router、angularjs url 中的 stateParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!