【问题标题】:Angularjs two stage resolve (resolve dependencies)Angularjs 两阶段解析(解决依赖关系)
【发布时间】:2016-07-25 06:00:15
【问题描述】:

我有一种状态,我需要先解决一项,然后才能获得解决另一项的数据:

      .state('addtrip', {
        url: '/addtrip',
        templateUrl: 'views/addtrip.html',
        controller: 'AddtripCtrl',
        resolve: {
          auth : function($state, Auth){
            return Auth.$requireAuth().then(function(auth) {
              return Auth;
            }, function(error){
              $state.go('login');
            });
          },
          trips : function(rootRef,$firebaseArray){
            return $firebaseArray(rootRef).child(auth.uid).$loaded();
          }
        }

所以首先我想获取 auth 对象,并且只有在我想检索该特定用户的行程之后。

处理这种情况的最佳方法是什么?

【问题讨论】:

    标签: javascript angularjs firebase angularfire firebase-authentication


    【解决方案1】:

    上面的那个人忘记在内部函数上返回一个承诺

    .state('addtrip', {
            url: '/addtrip',
            templateUrl: 'views/addtrip.html',
            controller: 'AddtripCtrl',
            resolve: {
              auth :function($state, Auth,rootRef,$firebaseArray,$q){
                 return Auth.$requireAuth().then(function(auth) {
                   var p = new Promise (resolve, reject)
                   resolve({
                     auth: auth,
                     trips:  $firebaseArray(rootRef).child(auth.uid).$loaded();
                   });
               return p;
              })
            }
    
        }
    }

    【讨论】:

      【解决方案2】:

      首先,我使用firebase website 中解释的方式,然后在您的trips 函数中使用currentauth 和waitforauth。您将不得不对其进行一些更改以适应您的程序,但我自己使用它,它的工作原理是这样的。 (抱歉代码缩进不好)

      .run(["$rootScope", "$state", function($rootScope, $state) {
          $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
              // We can catch the error thrown when the $requireAuth promise is rejected
              // and redirect the user back to the home page
              if (error === "AUTH_REQUIRED") {
                  $state.go("login");
              }
          });
      }])
      
      
        .state('addtrip', {
          url: '/addtrip',
          templateUrl: 'views/addtrip.html',
          controller: 'AddtripCtrl',
          resolve: {
            "currentAuth": ["firebaseRef", function (firebaseRef) {
                              // $requireAuth returns a promise so the resolve waits for it to complete
                              // If the promise is rejected, it will throw a $stateChangeError (see above)
                              //return firebaseRef.refAuth().$requireAuth();
                              return firebaseRef.refAuth().$requireSignIn();
                          }],
                          "waitForAuth": ["firebaseRef", function (firebaseRef) {
                              // $requireAuth returns a promise so the resolve waits for it to complete
                              // If the promise is rejected, it will throw a $stateChangeError (see above)
                              return firebaseRef.refAuth().$waitForSignIn();
                          }],
            trips : function(currentAuth, waitForAuth, rootRef,$firebaseArray){
              return $firebaseArray(rootRef).child(currentAuth.uid).$loaded();
            }
          }
      

      【讨论】:

        【解决方案3】:

        让我们尝试使用 promise 嵌套。它应该工作,

          .state('addtrip', {
            url: '/addtrip',
            templateUrl: 'views/addtrip.html',
            controller: 'AddtripCtrl',
            resolve: {
              auth :function($state, Auth,rootRef,$firebaseArray,$q){
                  var defer = $q.defer();
                  var obj = [];
                   Auth.$requireAuth().then(function(auth) {
                    obj.push(auth);
                    var a = $firebaseArray(rootRef).child(auth.uid).$loaded();
                    obj.push(a);
                    defer.resolve(obj);
               });
                return defer.promise;
              }
        }
        

        或,

         .state('addtrip', {
                url: '/addtrip',
                templateUrl: 'views/addtrip.html',
                controller: 'AddtripCtrl',
                resolve: {
                  auth :function($state, Auth,rootRef,$firebaseArray,$q){
                     return Auth.$requireAuth().then(function(auth) {
            return {
                    auth: auth,
                    trips:  $firebaseArray(rootRef).child(auth.uid).$loaded();
                   };
                  })
                }
        
        }
                }
        

        【讨论】:

        • 它不工作。他只解决了身份验证,但是当我添加旅行时解决了它的失败。
        • 已更新。最好使用承诺嵌套。
        猜你喜欢
        • 2016-11-11
        • 2014-12-18
        • 1970-01-01
        • 2014-05-06
        • 2019-05-19
        • 1970-01-01
        • 2016-02-16
        • 2017-02-20
        相关资源
        最近更新 更多