FB Like button shows up only once - on first load of my AngularJS view

假如想象 提交于 2019-12-03 15:09:55

I believe I found the solution.

Set in the begining of your widget script FB = null; and remove the "if (d.getElementById(id)) return;"

It should look like this:

FB = null;
(function(d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0];
    //if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/pt_BR/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

The accepted answer given by user2912405 it's perfect. I just wanted to add that if you are using AngularJS

You should use:

$scope.$on('$viewContentLoaded', function() {
    (function(d, s, id) {
        FB = null;
        var js, fjs = d.getElementsByTagName(s)[0];
        //if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=204269043065238";
        fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
});

To get the buttons re-loaded every time we change the view, for example when we follow a link

accepted answer worked for me, after much trial an error (fb root was ommitted). here is the full snippet to include in your view:

<div id="fb-root"></div>
<script>
    FB=null;
    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=xxxxxxxxx&version=v2.0";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>

also make sure to delete any code from index page

The accepted answer did not work for me on my Angular 5 project.

Adding this to my component class worked:

ngOnInit() {
    if (window['FB']) {
        window['FB'].XFBML.parse();
    }
}

Personally, I ended up with something like this. I have a component in angular 1.5. In index file, there is classic markup from documentation and inside the component

this.$onInit = function() {
    var facebookButtonEl = $element[0].querySelector('.fb-share-button');

    if (facebookButtonEl)
    angular.element(facebookButtonEl)
        .attr('href', $location.absUrl())
        .attr('data-href', $location.absUrl());

    if (window.FB) {
        window.FB.XFBML.parse();
    }
}

The important part is the FB.XFBML.parse() which envokes re-rendering of the button

IMHO, the accepted answer is not good answer. Following reasons:

  • You would end up inserting script multiple times.
  • window.FB = null is better syntax, instead of FB = null
  • If script already exists then don't add-fetch script again. Instead, manually do window.FB.XFBML.parse();.

    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {
           window.FB.XFBML.parse(); // Helps to identify FB elements again.
           return;
        }
        js = d.createElement(s); js.id = id;
    
    
        js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6"; // Put your Latest version here.
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    

FB.XFBML.parse

For those using opt-in cookie consent (for GDPR Law for example) and need to active for the first time programmatically the fb plugin:

var loadFB = (function (d, s, id) {
  //- call this function after receiving consent
  return function loadFB() {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (window['FB']) {
      window['FB'].XFBML.parse();
      return;
    }

    js = d.createElement(s);
    js.id = id;
    js.src = 'https://connect.facebook.net/fr_FR/sdk.js#xfbml=1&version=v3.0';
    fjs.parentNode.insertBefore(js, fjs);
  }
}(document, 'script', 'facebook-jssdk'));

In app.component.ts :

const onNavigationEnd = this.router.events.pipe(
  filter(event => event instanceof NavigationEnd),
  map((event: NavigationEnd) => {
    // ccService is the cookieConsentService
    if (this.ccService.hasConsented()) {

      // Set Google Analytics
      ga('set', 'page', event.urlAfterRedirects);
      ga('send', 'pageview');

      // Set Facebook sdk
      loadFB();

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