AngularJS + Facebook就像指令不起作用

sch*_*sch 5 html javascript facebook angularjs

我正在开发一个AngularJs应用程序,它在视图中集成了fb-like.它只适用于第一次,如果我也刷新浏览器,当我导航到其他视图并回来时插件不起作用.

我已经创建了一个指令.

angular.module('myApp.directives', []).directive('fbLike',['$timeout', function($timeout) {
return {
    restrict: 'A',
    scope: {},
    template: "<div class=\"fb-like-box\" data-href=\"{{page}}\" data-width=\"{{width}}\" data-show-faces=\"{{faces}}\" data-height=\"{{height}}\" data-show-border=\"{{border}}\" data-stream=\"{{stream}}\" data-header=\"{{header}}\"></div>",
    link: function($scope, $element, $attrs) {
        var working, _ref, _ref1;

        $scope.page ="https://www.facebook.com/company_name";
        $scope.border = false;
        $scope.height = (_ref = $attrs.fbHeight) != null ? _ref : '260';
        $scope.faces = $attrs.fbFaces != null ? $attrs.fbFaces : 'false';
        $scope.stream = $attrs.fbStream != null ? $attrs.fbStream : 'true';
        $scope.header = $attrs.fbHeader != null ? $attrs.fbHeader : 'false';
        $scope.width = (_ref1 = $attrs.fbWidth) != null ? _ref1 : $element.parent().width()-19;
        $timeout(function () { return typeof FB !== "undefined" && FB !== null ? FB.XFBML.parse($element.parent()[0]): void 0; });
    }
};}]);
Run Code Online (Sandbox Code Playgroud)

我的看法

    <script>

window.fbAsyncInit = function() {
 FB.init({

appId: '',
     status: true, // check login status
     cookie: true, // enable cookies to allow the server to access the session
     xfbml: true,  // parse XFBML
     oauth: true

   });

  };

(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/es_LA/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

</script>

<div fb-like></div>
Run Code Online (Sandbox Code Playgroud)

我正在寻找其他选择,但没有幸运.

我不知道什么是错的.

非常感谢

sch*_*sch 4

我发现的最佳解决方案是将 ezfb 模块添加到我的应用程序中,现在它工作得很好。

我完全推荐这个模块。

.config(function ($FBProvider) {
  $FBProvider.setInitParams({
    appId: '386469651480295'
  });  
})
Run Code Online (Sandbox Code Playgroud)

Plunker Demo上有一个演示

GitHub 链接