简体   繁体   English

使成为<script> tag in angularjs

[英]render <script> tag in angularjs

I'm trying to add paypal's pay button to my angularjs app,我正在尝试将 paypal 的支付按钮添加到我的 angularjs 应用程序中,

$scope.paypal ='<script async="async" src="https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=DJOEURGOJ97S"'+ 
              'data-button="buynow"'+
              'data-name="socks"'+
              'data-quantity="1"'+ 
              'data-amount="{{dollarValue}}"'+
              'data-currency="USD"'+ 
              'data-shipping="0"'+
              'data-tax="0"'+
              'data-callback="https://gamerhoic.com/ipn/data.php"'+
          '></script>';

It would be so awesome IF it was just that easy.如果就这么简单,那就太棒了。

I've tried a number of suggestions including adding ng-santize and $sce.trustAsHtml;我尝试了许多建议,包括添加 ng-santize 和 $sce.trustAsHtml;

$sce.trustAsHtml('<script></script>');

I read through Binding data in Angular js in string added via $sce.trustAsHtml thoroughly but it's a bit more complex than what I'm doing我彻底阅读了通过 $sce.trustAsHtml 添加的字符串中 Angular js 中的绑定数据,但这比我正在做的要复杂一些

using $sce.trustAtHtml nothing renders.使用 $sce.trustAtHtml 没有任何渲染。 If I add {{paypal}} I obviously get the $scope.paypal '' text displayed如果我添加 {{paypal}} 我显然会显示 $scope.paypal '' 文本

     <div bind-unsafe-html="paypal">{{paypal}}</div>

Best approach will be create a directive, then from in the directive you can use angular.element (basically jQuery lite) to add the script to the directive element.最好的方法是创建一个指令,然后在指令中您可以使用angular.element (基本上是 jQuery lite)将脚本添加到指令元素。 Following should work out well:以下应该效果很好:

angular.module('myApp')
.directive('paypal', [function(){
    return {
        scope: {
            dollarValue: '@'
        },
        link: function($scope, iElm, iAttrs, controller) {
            var script = '<script async="async" src="' + 
                'https://www.paypalobjects.com/js/external/' + 
                'paypal-button.min.js?merchant=DJOEURGOJ97S"'+ 
                'data-button="buynow"'+
                'data-name="socks"'+
                'data-quantity="1"'+ 
                'data-amount="' + $scope.dollarValue + '"'+
                'data-currency="USD"'+ 
                'data-shipping="0"'+
                'data-tax="0"'+
                'data-callback="https://gamerhoic.com/ipn/data.php"'+
                '></script>';

            var scriptElem = angular.element(script)
            iElm.append(scriptElem)
            scriptElem.on('ready', ...)
        }
    };
}]);

Notice this directive has isolate scope, and it is assigning it's dollarValue scope property to the string value of the parent's (so you can re-use directive)请注意,该指令具有隔离作用域,并且它将其dollarValue作用域属性分配给父级的字符串值(因此您可以重用指令)

Then in your view html, use the new custom directive as an element or attribute, specifying the dollarAmt :然后在您的视图 html 中,使用新的自定义指令作为元素或属性,指定dollarAmt

<paypal dollarAmt="{{dollarAmt}}"></paypal>

or或者

<div paypal dollarAmt="{{dollarAmt}}"></div>

You should see your script tag get appended to it.你应该看到你的脚本标签被附加到它上面。 You could also replace the containing element in the directive (reassign element in link fn to whatever you want.)您还可以替换指令中的包含元素(将link fn 中的element重新分配给您想要的任何element 。)

Check out this guide to creating custom directives , it's really cool看看这个创建自定义指令的指南,它真的很酷

UPDATE更新

Looks like to create these script tags it's more foolproof to use document.createElement() rather than angular.element ( see here )看起来要创建这些脚本标签,使用document.createElement()而不是angular.element更万无一失( 请参阅此处

I changed the directive above to use this code and was able to see the paypal button appear on the page:我更改了上面的指令以使用此代码,并且能够看到页面上出现贝宝按钮:

var scriptElem = angular.element(document.createElement('script'))
scriptElem.attr("src", scriptUrl) // set var appropriately
element.append(scriptElem)

I'm seen a lot of folks searching for a paypal and angularjs option... here's what I did to get it all working using the code from the awesome help I got above我看到很多人在寻找 paypal 和 angularjs 选项......这是我使用上面很棒的帮助中的代码来让它全部工作的方法

from the controller I broadcast the paypal amount从控制器我广播贝宝金额

$scope.$broadcast("paypalAmount", $scope.dollarValue)

using the provided directive I listen for the broadcast and update the amount for the payment button使用提供的指令我收听广播并更新付款按钮的金额

.directive('paypal', [function(){
return {
    scope: {},
    link: function($scope, element, iAttrs) {

                $scope.$on(
                    "paypalAmount",
                    function handlePingEvent( event, dollarValue ) {

                        var scriptElem = angular.element(document.createElement('script'))
                        scriptElem.attr({src:'https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=YOUR-PAYPAL-EMAIL','data-button':'buynow','data-name':'Arcade Tokens','data-amount':dollarValue,'data-currency':'USD','data-callback':'https://gamerholic.com/ipn/data.php' }) // set var appropriately
                        element.append(scriptElem)

                    }
                );

    }
};
}]);

I have created a module to overcome this problem.我创建了一个模块来克服这个问题。 Please take a look at: https://github.com/umair321/Inpage-js-render-for-angularjs请看: https : //github.com/umair321/Inpage-js-render-for-angularjs

Very much easy to use:非常容易使用:

Install it using bower: bower install inpage-js-render-angular1使用 bower 安装它: bower install inpage-js-render-angular1

Just include js-render.js to your page只需将 js-render.js 包含到您的页面中

Add 'ngLoadScript' to your app dependencies.将“ngLoadScript”添加到您的应用依赖项。 Eg angular.module('YOUR_APP_NAME', ['ngLoadScript'])例如 angular.module('YOUR_APP_NAME', ['ngLoadScript'])

Used it as:将其用作:

<script type="text/javascript-inpage"> 
  /**Your Script here**/ 
</script>

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

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