I am trying to connect a facebook sharing plugin. The problem I am facing is that I need to reload the page in order to actually get the share button. If I go to the page using the link or url, the facebook share button will not appear, I need to reload the page, and then the button will appear. I use angular, so I thought to use the directive, but so far my efforts have not been successful.
this is where i have the directive in my template
<div class="fb-share-button" fb-share data-href="{{fbUrl}}" data-layout="button" id='fb-share'></div>
here is my directive.
angular.module('App').directive('fbShare', function () { function createHTML(href, layout) { return '<div class="fb-share-button" ' + 'data-href="' + href + '" ' + 'data-layout="' + layout + '" ' + '</div>'; } return { restrict: 'A', scope: {}, link: function postLink(scope, elem, attrs) { attrs.$observe('dataHref', function (newValue) { var href = newValue; var layout = attrs.layout || 'button'; elem.html(createHTML(href, layout)); FB.XFBML.parse(elem[0]); }); } }; });
facebook sdk code that immediately after the opening tag tag
<div id="fb-root"></div> <script>(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/en_US/sdk.js#xfbml=1&version=v2.5&appId=xxxxxxxxxx"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script>
javascript angularjs facebook
Billy
source share