Placing Amazon Banner | Angular V4

淺唱寂寞╮ 提交于 2019-12-07 20:45:28

问题


I'm placing an Amazon banner inside an angular material 2 card.But the problem is that it is not rendering.It shows empty div.What could be the reason.Below is the code showing how I did it.

<md-card class="full-width full-height border-box ">
    <div class="adv">
        <script type="text/javascript" language="javascript">
            var aax_size = '728x90';
            var aax_pubname = 'XXXXXXXXXXX';
            var aax_src = '302';
          </script>
          <script type="text/javascript" language="javascript" src="http://c.amazon-adsystem.com/aax2/assoc.js"></script>
    </div>
  </md-card>

I also tried to to bind it using property binding

<span [innerHTML]="advertisement()"></span>
advertisement(){
  return `<div class="adv">
        <script type="text/javascript" language="javascript">
            var aax_size = '728x90';
            var aax_pubname = 'XXXXXXXXXXX';
            var aax_src = '302';
          </script>
          <script type="text/javascript" language="javascript" src="http://c.amazon-adsystem.com/aax2/assoc.js"></script>
    </div>`;

}

I also tried to dynamically add the div inside my card,it shows inside the div but the banner doesn't appear.Below is the code showing how I did that.

ngAfterViewInit() {
    let x: HTMLElement = document.getElementById('adv');
    let s: HTMLScriptElement = document.createElement('script');
    s.type = 'text/javascript';
   // s.language = 'javascript';
    let code = `var aax_size = '728x90';
                var aax_pubname = 'XXXXXXX';
                var aax_src = '302';`;
    let src = document.createElement('script');
    src.type = 'text/javascript';
   // src.language = 'javascript';
    src.src = 'http://c.amazon-adsystem.com/aax2/assoc.js';             
    try {
        s.appendChild(document.createTextNode(code));
        x.appendChild(s);
        x.appendChild(src);
    } catch (e) {
        s.text = code;
        document.body.appendChild(s);
    }
    console.log(x);
}

回答1:


After scrapping every post in SO regarding or similar to this question I did not find any solution to this.I followed almost everything in those posts but nothing was working for me.After that I came across postscribe library which does the externally loading of any third party script.

First I installed the library and imported it in my component

import * as postscribe from 'postscribe';

After that all I did was calling a function inside my ngAfterViewInit function, by targetting the div with its id which in my case was adv and passed the script as a second parameter to this function.

ngAfterViewInit() {
postscribe('#adv', `<script type="text/javascript" language="javascript">
                       var aax_size='728x90';
                       var aax_pubname = 'XXXXXXXX';
                       var aax_src='302';
                    </script>
                    <script type="text/javascript" language="javascript" src="http://c.amazon-adsystem.com/aax2/assoc.js"></script>`);}

By doing this my banner was loaded.



来源:https://stackoverflow.com/questions/44606780/placing-amazon-banner-angular-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!