2016-12-26 146 views
0

我正在尝试在Angularjs中调用服务方法。服务方法称为好,但是IT不会将任何值返回给在控制器中调用它的函数。

如果有人能帮助我,我会很高兴。这是我的代码波纹管。

logosStreams.factory("processPaypal", ['$http', '$timeout', '$q', '$state',function($http, $timeout, $q, $state){ 
    var transCall = {}; 
    var transcallPromise2 = $q.defer(); 

    return { 

    onSuccesfulPayment: function(payment) { 
     console.log("payment success: " + JSON.stringify(payment, null, 4)); 
transcallPromise2.resolve(payment); 
    }, 
    onAuthorizationCallback: function(authorization) { 
     console.log("authorization: " + JSON.stringify(authorization, null, 4)); 
     //return authorization; 
    }, 
    createPayment: function(data) { 
     // for simplicity use predefined amount 
     var paymentDetails = new PayPalPaymentDetails("0", "0", "0"); 
     var payment = new PayPalPayment(data.amt, "USD", data.name, "Sale", 
     paymentDetails); 
     return payment; 
    }, 

    buyInFutureBtn : function(e) { 
     // future payment 
     PayPalMobile.renderFuturePaymentUI(this.onAuthorizationCallback, this.onUserCanceled); 
    }, 

    profileSharingBtn : function(e) { 
     // profile sharing 
     PayPalMobile.renderProfileSharingUI(["profile", "email", "phone","address", "futurepayments", "paypalattributes"], 
     this.onAuthorizationCallback, this.onUserCanceled); 
    }, 

    buyNowBtn : function(data) { 

     // single payment 
     PayPalMobile.renderSinglePaymentUI(this.createPayment(data), this.onSuccesfulPayment, this.onUserCanceled); 
     return transcallPromise2.promise; 

    }, 

    onPrepareRender: function() { 

    }, 

    onUserCanceled: function(result) { 
     console.log(result); 
       transcallPromise2.reject(result); 

    } 

} 

}]) 

内部控制呼叫buyNowBtn methond

processPaypal.buyNowBtn($scope.MMParams).then(function(response){ 
      console.log(response); 
    }) 
+0

您确定该方法没有返回承诺吗?如果你执行'console.log(processPaypal.buyNowBtn($ scope.MMParams))',会发生什么? 可能是承诺被拒绝或者没有得到解决。 – ppham27

+0

控制台中是否有错误? –

+0

@CharanCherry是的,有时我会得到:“TypeError:无法读取未定义的属性'then' –

回答

0

其中一个可能的原因是它没有返回值是你的承诺没有得到解决。您的承诺只有在您致电SuccessSupport()时才能得到解决。 你也可以把你的onSuccessfulPayment()函数执行的代码。

0

您已返回buyNow函数执行中的承诺对象...尝试将返回的承诺作为您返回的所有函数连接的返回对象的一部分。

+0

请你可以有一个实际的例子。我没有得到你的答案,谢谢 –