UI路由器嵌套视图无法解析

时间:2016-05-15 04:55:50

标签: angularjs angular-ui-router angular-ui angular-promise

我正在使用UI Router Multiple Views概念。

文档说我可以对个人观点resolve。正如您在my plunker中所看到的,我对孩子的观点有了决心。解析返回一个promise,在我的例子中,在父控制器中解析。我用setTimeOut模拟了我的问题。基本上,在5秒后,子视图应该加载(带有“In Child State”的警告),但它没有!

相关代码如下,但请参阅Plunker。

'child@parent': {
    template: '<h4>This is Child State</h4>',
    controller: childCtrl,
    resolve: {
        trd: function(tradeFactory) {
            console.log('in resolve of child');
            return tradeFactory.ready();
        }
    }
 }

1 个答案:

答案 0 :(得分:1)

(检查更新并正常工作plunker这里的重点是:

  

所有resolve必须已经解决,才能呈现(及其控制器)的视图

在您的方案中情况并非如此。事实上,存在竞争条件,最终陷入僵局。

  1. 一个视图正在等待解决(提供 promise
  2. 其他视图的控制器已准备好触发解决(做出承诺已解决,但是...等待其兄弟姐妹时无法触发
  3. 原始摘录

    views: {
    
      '': {
        ...
        // inside of this, you expect to resolve the below promise
        controller: parentCtrl
      },
    
    
      'child@parent': {
          ...
          resolve: {
              trd: function(tradeFactory) {
                  // this is a promise waiting for resolve
                  // implemented in parent controller ... deadlock
                  return tradeFactory.ready();
              }
          }
      }
    

    所以,上面的说法不行。我们必须在决心内做所有事情,例如:这样:

    views: {
    
      '': {
        ...
        controller: parentCtrl,
        // here do the resolve
        resolve: {
          parentReady: function(tradeFactory){
            setTimeout(function(){ 
              tradeFactory.ResolveReady()
            }, 1500); 
          }
        }
      },
    
    
      'child@parent': {
          ...
          resolve: {
              trd: function(tradeFactory) {
                  console.log('in resolve of child');
                  return tradeFactory.ready();
              }
          }
      }
    

    updated plunker