2016-12-26 144 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

請你可以有一個實際的例子。我沒有得到你的答案,謝謝 –