抽象状态的 url 中的 stateParams,ui-router,angularjs抽象、状态、url、stateParams

由网友(稍安勿躁?)分享简介:如果我有一个抽象状态:if i have an abstract state:$stateProvider.state('drug', {abstract: true,url: '/drug/:drugId',template:'',resolve: {drugId : ['$...

如果我有一个抽象状态:

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.y and 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>

阅读全文

相关推荐

最新文章