【问题标题】:Use response from previous resolve object使用来自先前解析对象的响应
【发布时间】:2015-01-20 05:24:10
【问题描述】:

我有一个带有多个属性的解析对象的 angularjs 路由,如下所示:

.state('user', {
                url: '/user/signup',
                controller: 'CreateAccountCtrl',
                templateUrl: 'createaccount.tpl.html',
                resolve: {
                    practice: {
                        // Returns a promise
                    },
                    someOtherFunction: {
                        // Returns a promise, needs resolved object from practice
                    },
                }
            })

问题是我需要其中一个解析的结果来处理另一个解析。我该如何实施?显然,我可以将所有 http 调用放在一个函数中并构建一个自定义对象,但我想知道是否有更惯用的解决方案。

【问题讨论】:

    标签: angularjs promise angular-ui-router


    【解决方案1】:

    resolve 方法的两个属性都转换为函数,并通过名称将一个方法作为参数传递给另一个。

    .state('user', {
      url: '/user/signup',
      controller: 'CreateAccountCtrl',
      templateUrl: 'createaccount.tpl.html',
      resolve: {
        practice: function() {
            // Returns a promise
        },
        someOtherFunction: function(practice) {
            // Returns a promise, needs resolved object from practice
        }
      }
    })
    

    另外,this blog post 是使用 Angular UI 路由器的绝佳资源。我在那里学到了这种方法。

    【讨论】:

      【解决方案2】:

      使用IIFE 返回resolve 对象:

      ...
      resolve: (function () {
          var practicePromise;
          var someOtherPromise;
      
          /* initialize the variables whichever way you want*/
      
          return { /* this is the actual "resolve" object*/
      
              practice: practicePromise,
              someOther: someOtherPromise
          };
      } ()),
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-12
        • 2019-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多