简体   繁体   English

html完成加载之前,angularjs承诺和外部braintree.js脚本未获得价值

[英]angularjs promise and external braintree.js script not getting value before html finished loading

I have to use external braintree.js script in order to generate a payment widget. 我必须使用外部braintree.js脚本才能生成付款小部件。 I have no control over this. 我对此无能为力。 Here's the code for it that goes into my .html page: 这是进入我的.html页面的代码:

 <div id="myClient" ng-show="false">{{myClientToken}}</div> <form id="checkout" action="actionURL" method="post" enctype="multipart/form-data"> <div id="payment-form"></div> <input type="hidden" name="amount" value="{{balance}}" /> <input type="hidden" name="uuid" value="{{uuid}}" /> <input type="hidden" name="ptype" value="custom" /> <input type="submit" id="paypal" value="Pay Now"> <!--form not shown to the user--> </form> <script src="https://js.braintreegateway.com/v2/braintree.js"></script> <script> $(document).ready(function() { var clientToken = $('#myClient').html(); braintree.setup(clientToken, "dropin", { container: "payment-form" }); }); </script> 

And in my controller I am successfully generating the token from my REST server: 在我的控制器中,我成功地从REST服务器生成了令牌:

  myFactory.getToken() .then(function(token) { $scope.myClientToken = token.data; 

However, as you might expect, the html finishes loading faster than the token gets returned to the controller. 但是,正如您可能期望的那样,HTML完成加载的速度超过了将令牌返回给控制器的速度。

This results in "misconfiguration" error, obviously, because braintree.js script expects the token which it does not get at the time it's called. 显然,这导致“配置错误”错误,因为braintree.js脚本期望在调用时没有获得的令牌。

I tried ng-init and ng-if on the div, but to no avail. 我在div上尝试了ng-init和ng-if,但无济于事。

Chain the startup of braintree after the retrieval of the token. 布伦特里的启动令牌的获取之后。

myFactory.getToken()
    .then(function(token) {
         $scope.myClientToken = token.data;
         return token.data;
     }) .then(function (clientToken) {
         braintree.setup(clientToken, "dropin", {
         container: "payment-form"
         });
     };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM