This is probably due to the fact that the FB functionality begins before Angular can change the data-href attribute.
The directive seems to be a good choice here:
You basically need to create a comment block after Angular, which can provide the correct URL.
Since this is due to asynchronous manipulation of the DOM, you need to use FB.XFBML.parse() so that the FB processes the comment block after changing the data-href attribute.
Directive:
.directive('dynFbCommentBox', function () { function createHTML(href, numposts, colorscheme) { return '<div class="fb-comments" ' + 'data-href="' + href + '" ' + 'data-numposts="' + numposts + '" ' + 'data-colorsheme="' + colorscheme + '">' + '</div>'; } return { restrict: 'A', scope: {}, link: function postLink(scope, elem, attrs) { attrs.$observe('pageHref', function (newValue) { var href = newValue; var numposts = attrs.numposts || 5; var colorscheme = attrs.colorscheme || 'light'; elem.html(createHTML(href, numposts, colorscheme)); FB.XFBML.parse(elem[0]); }); } }; });
HTML:
<div id="fb-comment-box" dyn-fb-comment-box page-href="https://example.com/page/{{page.id}}" numposts="5" colorscheme="light"> </div>
<sub> Note:
The scope of the directive will constantly monitor changes to the page-href attribute and update the comment block. You can change this according to your needs (for example, monitor changes to other attributes or bind them once and stop viewing). Sub>
See also this short demo .
source share