【问题标题】:Angular UI router - can't get my data in component after resolveAngular UI路由器-解析后无法在组件中获取我的数据
【发布时间】:2017-01-04 22:14:22
【问题描述】:

我有一个这样配置的路由器:

.state('home.view-profile', {
    url: '/view-profile/:profileUrl',
    template: '<view-profile input="$resolve.profileData"></view-profile>',
    title: 'View Profile',
    resolve: {profileData: function(artistService, $stateParams){
      return artistService.getArtist($stateParams.profileUrl).then((data) => {
        const timer = new Date();
        console.log(timer.getSeconds() + ':' + timer.getMilliseconds());
        console.log(data.data.artist);
        return data.data.artist;
      });
    }
  }
  })

和组件

module.component('view-profile', {
templateUrl: 'view-profile/view-profile.component.html',
    bindings: {
      input: '='
    },
    controller: ['$state', '$scope', '$stateParams', function ($state, $scope, $stateParams) {
      const model = this;
      console.log('***************');
      console.log(model.input);
   }]
});

我可以看到data.data.artist 在解析过程中是有效的并且可以从日志中访问,但在组件内部input 始终是undefined J只是不明白为什么会这样?

【问题讨论】:

  • 也许你应该将$resolve 注入你的控制器
  • 我试过了,但没有任何有意义的数据..

标签: javascript angularjs angular-ui-router angular-components


【解决方案1】:

您是否尝试过在控制器中解决承诺?

model.input.then(function(res) { console.log(res); }

【讨论】:

  • 不,那没用...找不到提供者 profileData
  • 呃。我太快了,没有仔细阅读你的代码,看不到你在传递东西。尝试解决控制器中的承诺,看看是否有效。
  • 我可以,但由于 model.input 是“未定义”,而且我确信它会出现错误...
【解决方案2】:

抱歉,感谢您的帮助! 我刚刚发现了这个问题。在我们的多开发人员工作流程中,ui-router 的版本以某种方式降级。一些开发人员安装了0.2.1 而不是最新的0.3.x,我合并了,这就是问题所在。 只有0.3.x 路由器支持将$resolve.param 发送到组件绑定。
升级后 - 一切正常。只有我的自信心受到伤害……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2015-08-19
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2017-01-14
    相关资源
    最近更新 更多