角UI路由器和访问父子状态PARAMS

角UI路由器和访问父子状态PARAMS

本文介绍了角UI路由器和访问父子状态PARAMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

更多的细节编辑

Edited with more details

新手的问题在这里,我可以肯定。我试图做一些事情,我认为将是简单的,但我没有得到任何地方。

Newbie question here, I am sure. I am trying to do something that I thought would be straightforward, but I am not getting anywhere.

基本上,我有几个孩子状态的父状态。我想在父状态的决心加载一些数据,以便它是提供给所有子状态。要做到这一点,我需要被传递到子状态是可用的参数(上下文)。我通过$ state.params得到它。在code(有些为清楚起见省略)是这样的:

Basically, I have a parent state with several child states. I want to load some data in the resolve of the parent state so that it is available to all child states. To do this, I need a parameter (context) that is passed to the child state to be available. I am getting it via $state.params. the code (some omitted for clarity) is like this:

.state('pbr', {
    url: "/pbr",
    templateUrl: "pbr/pbr.html",
    controller: "pbrBaseCtrl",
    abstract: true ,
    resolve: {
        dpts: function(NavDPT, $state) {
               return NavDPT.query({context: $state.params.context}).$promise;
              }
        }

})
.state('pbr.review', {
        url: "/review/{context}?div",
        templateUrl: "pbr/pbr-review.html",
        controller: "pbrReviewCtrl"
    })

据我了解,$ state.params应包含父母和孩子的状态传递的参数,而$ stateParams只为激活状态的PARAMS。

From what I understand, $state.params should contain the passed parameters for the parent and child states whereas $stateParams have the params only for the active state.

不管怎样,我所看到的是,决心发生之前$ state.params没有得到填充,因而数据失败取。它最终会虽然填充。我似乎无法制定出一个方法来等待$ state.params是获取数据之前可用。我曾尝试在超时等包裹的东西。

Anyway, what I am seeing is that $state.params does not get populated before the resolve happens, and thus the fetch of data fails. It does eventually get populated though. I can't seem to work out a way to wait for $state.params to be available before getting the data. I have tried wrapping the thing in a timeout, etc.

我曾尝试改用查询参数,并指定在父级别的查询参数,我观察此相同的行为 - 然而,当决心发生newcontext不可用

I have tried to instead use query params, and have specified the query param at the parent level, and I observe this same behavior - newcontext is not available yet when the resolve happens.

 .state('pbr', {
            url: "/pbr?newcontext",
            templateUrl: "pbr/pbr.html",
            controller: "pbrBaseCtrl",
            abstract: true ,
            resolve: {
                dptsResource: 'NavDPT',
                dpts: function(dptsResource, $state, $log) {
                    $log.info("state.params is also ",$state.params);
                    return dptsResource.query({context: $state.params.newcontext}).$promise;
                }
            }

        })

感谢。

克里斯

推荐答案

有的展示我们如何存取权限的 $ stateParams 即使在解析的功能,但它们必须被定义的电流的或的的状态 (不是孩子)

There is an example showing how we can acces the $stateParams even in resolve function, but they must be defined in current or parent state (not in child)

那么,这些都是状态:

  $stateProvider
    .state('pbr', {
        // we define 2 params oldContext and newContext
        url: "/pbr/{oldContext}?newContext",
        ...
        resolve: {
            // here we can get all params defined for our state
            dpts: function($state, $stateParams) {
                return {
                  oldContext : $stateParams.oldContext,
                  newContext : $stateParams.newContext,
                }
            }
        }
    })
    // child will have even the context as 3rd param
    .state('pbr.review', {
      url: "/review/{context}",
      ...
    })

这是如何调用它们的方法:

These are the ways how to call them:

// via ui-sref
ui-sref="pbr.review({oldContext: 'abc1', newContext : 'def1', context : 'xyz1'})"
ui-sref="pbr.review({oldContext: 'abc2', newContext : 'def2', context : 'xyz2'})"

// via href
href="#/pbr/abc1/review/xyz1?newContext=def1"
href="#/pbr/abc2/review/xyz2?newContext=def2"

观察到更多的在

这篇关于角UI路由器和访问父子状态PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:39