Instagram embeds not working when adding embeds dynamically

前端 未结 3 1303
南旧
南旧 2020-12-07 00:44

Working on a blog that loads posts on an infinite scroller. Each blog post may or may not have Instagram embeds. I\'m finding that the first one that shows on the page wil

相关标签:
3条回答
  • 2020-12-07 01:29

    In your app.js create a directive for adding script element:

    .directive('externalscript', function() {
       var injectScript = function(element) {
        var instagramScript = angular.element(document.createElement('script'));
        instagramScript.attr('async defer');
        instagramScript.attr('charset', 'utf-8');
        instagramScript.attr('src', 'http://platform.instagram.com/en_US/embeds.js');
        instagramScript.attr('onload', 'window.instgrm.Embeds.process()');
        element.append(instagramScript);
        var twitterScript = angular.element(document.createElement('script'));
        twitterScript.attr('async defer');
        twitterScript.attr('charset', 'utf-8');
        twitterScript.attr('src', 'http://platform.twitter.com/widgets.js');
        element.append(twitterScript);
    };
    
      return {
          link: function(scope, element) {
              injectScript(element);
          }
      };
    })
    

    and then in your html view call:

    <div externalscript></div>
    
    0 讨论(0)
  • 2020-12-07 01:33

    Inserting this into header and footer area worked for me in WordPress blog.

    <script async="" defer="defer" src="//platform.instagram.com/en_US/embeds.js"></script>

    0 讨论(0)
  • 2020-12-07 01:37

    The embed.js script that comes with Instagram's embed code has a process function you can call.

    window.instgrm.Embeds.process()
    

    Just use that whenever your dynamic content loads.

    0 讨论(0)
提交回复
热议问题