简体   繁体   English

Angular 1.5-UI-Router,解析对象返回未定义的值

[英]Angular 1.5 - UI-Router, Resolve Object returning undefined values

I am unable to get resolved data (via resolve object) from ui router pre-loaded into a component controller. 我无法从预加载到组件控制器的ui路由器获取解析数据(通过resolve对象)。 the properties are showing up in the component's this object, but all properties are undefined. 属性显示在组件的this对象中,但所有属性均未定义。 Why is this happening? 为什么会这样呢? I have been stuck on this for a while now... 我已经被卡住了一段时间了...

here is the component: 这是组件:

  .component('wLoans', {
    template: require('./loans.html'),
    controller: LoansController,
    bindings: {
      settings: '<',
      labels: '<'
    }
  });

this.settings and this.labels appear, but they are undefined. 将显示this.settings和this.labels,但是它们是未定义的。 when I console.log inside resolve.settings below, I can see the promise itself. 当我在下面的resolve.settings内进行console.log时,我可以看到promise本身。 in the promise below, value contains the return data I want: 在下面的承诺中, value包含我想要的返回数据: 在此处输入图片说明

below is the state I am working on with UI router: 以下是我正在使用UI路由器的状态:

{
      name: 'loans',
      parent: 'app',
      url: '/loans',
      data: {
        authRequired: true
      },
      views: {
        content: {
          template: '<w-loans></w-loans>'
        }
      },
      resolve: {
        labels(LabelService) {
          'ngInject';
          return LabelService.fetch();
        },
        settings(SettingsService) {
          'ngInject';
          console.log(SettingsService.fetch());
          return SettingsService.fetch()
        },
        module($q, $ocLazyLoad, LabelService) {
          'ngInject';
          return $q((resolve) => {
            require.ensure([], (require) => {
              let mod = require('pages/loans');
              $ocLazyLoad.load({ name: mod.name });
              resolve(mod.name, LabelService.fetch());
            }, 'loans');
          });
        }
      }
    }

here is the fetch function from the service that's being called in the route: 这是从路由中调用的服务获取的函数:

function fetch() {
        // return $http.get(require('!!file!mocks/settings.json'), {
    return $http.get(`${DEV_API_URL}/settings`, {
      cache: settingsCache,
      transformResponse: [
        ...$http.defaults.transformResponse,
        transformResponse
      ]
    })
      .then(({ data }) => {
        angular.extend(model, data);
        settingsCache.put('store', model);
        return model;
      });
  }

Any help is appreciated! 任何帮助表示赞赏!

UPDATE: using ui router 1.0.0-beta.1 更新:使用ui路由器1.0.0-beta.1

As long as you are not using the 1.0.0 (currently beta) version of the ui-router there is no way to directly use the components. 只要您不使用ui-router的1.0.0(当前为beta)版本,就无法直接使用这些组件。 For the beta version, please see this discussion: https://github.com/angular-ui/ui-router/issues/2627 . 对于beta版本,请参见以下讨论: https : //github.com/angular-ui/ui-router/issues/2627 There is also a tutorial on how to use routing to components with resolves: https://ui-router.github.io/tutorial/ng1/hellogalaxy 还有一个关于如何使用带有解析功能的组件进行路由的教程: https : //ui-router.github.io/tutorial/ng1/hellogalaxy

What you are doing here is creating a state named loans with an implicit controller and scope which will have the resolved data assigned. 您在这里所做的是创建一个具有loans状态的状态,该状态具有隐式控制器和范围,并将分配已解析的数据。 In the template you then instantiate your component without passing any of the attributes required. 然后,您可以在模板中实例化组件,而无需传递任何所需的属性。

So if you want to use your component with the 0.2.x ui-router, you need to add some dummy controller which gets the resolved properties injected and then pass the values of the dummy controller into the component via the template like 因此,如果要将组件与0.2.x ui路由器一起使用,则需要添加一些虚拟控制器,该虚拟控制器获取已注入的已解析属性,然后通过模板将虚拟控制器的值传递到组件中

<w-loans settings="settings" .../>

Depending on how new your version of the ui-router is (0.3.x?), you can probably use the newly introduced $resolve property to avoid creating a dummy controller like this: 根据您的ui-router版本(0.3.x?)有多新,您可以使用新引入的$resolve属性来避免创建虚拟控制器,如下所示:

<w-loans settings="$resolve.settings" ...></w-loans>

Details here: https://github.com/angular-ui/ui-router/issues/2793#issuecomment-223764105 此处的详细信息: https : //github.com/angular-ui/ui-router/issues/2793#issuecomment-223764105

I had a similar problem with Angular 1.6 and it looks like a different behavior of this version. 我在Angular 1.6中遇到了类似的问题,它看起来与此版本的行为不同。 My view is rendered after the promise is resolved, but the controller is called before, I think. 我的看法是在诺言解决之后呈现的,但我认为控制器是在调用之前被呈现的。 It works for me using $onInit like the angular docs suggests: 它像角度文档建议的那样使用$ onInit为我工作:

.component('myComponent', {
  bindings: {value: '<'},
  controller: function() {
    this.$onInit = function() {
      // do somthing with this.value
    };
  }
})

https://github.com/angular/angular.js/issues/15545 https://github.com/angular/angular.js/issues/15545

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM