2016-11-04 142 views
1

我不知道如何去回复承诺。我试图在嵌套方法返回的结果,但更愿意返回结果有两种不同的方法如图所示算账:

$scope.relatedContacts = function (accountId) { 
       if (!lodash.isNil(accountId)) { 
        try { 
         return restangular.one('user') 
         .one('contactimages') 
         .get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId }) 
         .then(function (response) { 
          return response.data;}); 
       } 
      } 

宁愿解决下面的例子:

$scope.relatedContacts = function (accountId) { 

         if (!lodash.isNil(accountId)) { 

          try { 
           var deferred = $q.defer(); 
            return restangular.one('user') 
            .one('contactimages') 
            .get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId }) 
            return deferred.promise; 
           } 
          catch (err) { 
           $scope.contactsPopulated = false; 
          } 
         } 
        } 

    $scope.relatedContacts().then(function (response) { 
      //Some logic here   
    } 

目前我我越来越:“类型错误:无法读取属性‘然后’未定义 的”

感谢所有

回答

0

首先,记得consisten CY。 。该isNil如果让你的函数不是在某些情况下返回任何东西时,不提供accountId(你会得到TypeError: Cannot read property 'then' of undefined "错误

有两种方法来解决你的问题

第一种方式:

$scope.relatedContacts = function (accountId) { 
    return $q(function(resolve, reject) { 
    if (!lodash.isNil(accountId)) { 
     try { 
     return restangular.one('user') 
      .one('contactimages') 
      .get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId }) 
      .then(function(response) { 
      resolve(response.data) 
      }, reject); 
     } 
     catch (err) { 
     $scope.contactsPopulated = false; 
     reject(err); 
     } 
    } 
    }); 
}; 

的第二种方式(使用延迟)。

$scope.relatedContacts = function (accountId) { 
    var def = $q.defer(); 
    if (!lodash.isNil(accountId)) { 
    try { 
     restangular.one('user') 
     .one('contactimages') 
     .get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId }) 
     .then(function(response) { 
      def.resolve(response.data) 
     }, def.reject); 
    } 
    catch (err) { 
     $scope.contactsPopulated = false; 
     def.reject(err); 
    } 
    } 
    return def; 
}; 

您应该检查官方参考有关$q服务: https://docs.angularjs.org/api/ng/service/$q

有典型的诺言用途的例子不胜枚举。

+0

对于第一种方法,我添加$ Q在我的控制器,但现在收到错误:类型错误:$ q是不是一个函数:.controller( 'SelectUserController',[ '$范围', 'PubSub的',“Pnotify ','Common.InfiniteListService','SelectUserService','Restangular','$ q', 函数($ scope,pubSub,pnotify,InfiniteListService,selectUserService,restangular,$ q){ –

+0

问题是我想要执行一些逻辑与响应:例如;当承诺被解决时,我想检查额外的逻辑。 –

+0

isNil将工作,因为我只有在帐户不为空时调用方法。 –

0

这是我用来返回值并添加额外的方法来处理响应。

$scope.relatedContacts = function (accountId) { 

         var deferred = $q.defer(); 
         if (!lodash.isNil(accountId)) { 

          try { 

           deferred.resolve(restangular.one('user') 
            .one('contactimages') 
            .get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId })); 

          } 
          catch (err) { 
           $scope.contactsPopulated = false; 
           deferred.reject(err); 
          } 
         } 
         deferred.promise.then(function (response) { 
          var tests = response; 
          return $q.when(); 
         }, 
         function() { 
          console.log("1st reject"); 
          return $q.reject(); 
         }); 
         return deferred.promise; 

        };