使用 angularjs 调用正常 javascript

call normal javascript using angualrjs

我正在尝试将用于联盟跟踪的遗留 javascript 代码包含到 angularjs 应用程序中

<script type="text/javascript">
        //<![CDATA[ 

        /*** Do not change ***/
        var AWIN = {};
        AWIN.Tracking = {};
        AWIN.Tracking.Sale = {};

        /*** Set your transaction parameters ***/
        AWIN.Tracking.Sale.amount = '{{order_total}}';
        AWIN.Tracking.Sale.orderRef = '{(order_id}}';
        AWIN.Tracking.Sale.parts = '{{cats}}';
        AWIN.Tracking.Sale.voucher = '';
        AWIN.Tracking.Sale.currency = 'GBP';
        AWIN.Tracking.Sale.test = '0';
        AWIN.Tracking.Sale.channel = 'aw';
        //]]>
    </script>

我需要调用上面的代码(用 angualr 变量填充占位符)然后调用这个:

    <script src="https://www.dwin1.com/xxxx.js" type="text/javascript" defer="defer"></script>

最好的方法是什么?我尝试将代码放入 Angular 函数但没有成功我还尝试将 vars 向下传递到 $window 并尝试在我最后将它们插入 CDATA 但没有成功。有什么想法吗?


我应该将此代码添加到 HTML 页面中,该页面包裹在 angualrjs 控制器中

angularjs 无法直接实现您的尝试。你需要做如下的事情

//Note: this is a new JS file included in your main html

(function() {
    "use strict";

    window.AWIN = {};

    window.populateAwin = function(args) {
        //<![CDATA[ 

        /*** Do not change ***/
        AWIN.Tracking = {};
        AWIN.Tracking.Sale = {};

        /*** Set your transaction parameters ***/
        AWIN.Tracking.Sale.amount = args.order_total;
        AWIN.Tracking.Sale.orderRef = args.order_id;
        AWIN.Tracking.Sale.parts = args.cats;
        AWIN.Tracking.Sale.voucher = '';
        AWIN.Tracking.Sale.currency = 'GBP';
        AWIN.Tracking.Sale.test = '0';
        AWIN.Tracking.Sale.channel = 'aw';
        //]]>
    }

}());

然后您需要通过传递所需数据从您的控制器调用此函数,如下所示

populateAwin({
   order_total: $scope.order_total,
   order_id: $scope.order_id,
   cats: $scope.cats
});

完成后,您可以继续从您的控制器中动态包含脚本,如下所示

var scriptEl = document.createElement("script");
scriptEl.type = "text/javascript";
scriptEl.defer = "defer";
scriptEl.src = "https://www.dwin1.com/xxxx.js";

document.body.appendChild(scriptEl);