使用来自先前解析对象的响应

Use response from previous resolve object

我有一个 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 调用放在一个函数中并构建一个自定义对象,但我想知道是否有更惯用的解决方案。

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 路由器的重要资源。我在那里学到了这种方法。

使用 IIFE 到 return 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
    };
} ()),
...