单页应用程序无法获取facebook共享的当前页面元标记

Joh*_*Lim 11 html javascript facebook angularjs

我正在尝试在我的应用程序中实现facebook分享按钮,但是我似乎无法让它继续工作并且idk为什么这是我到目前为止所拥有的.

HTML(/后/一些随机字符串)

<div id="fb-root"></div>
<meta property="og:site_name" content="App">
<meta property="og:url" content="/post/{{data.permalink}}">
<meta property="og:title" content="{{data.title}}">
<meta property="og:type" content="blog">
<meta property="og:image" content="https://i1.ytimg.com/vi/tIWPnxEpNQg/maxresdefault.jpg"> 
<meta property="og:description" content="{{data.preview}}">

<body >
<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&appId=580882498674160&version=v2.0";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
   // this facebook share button doesn't appear.
   <div class="fb-share-button" data-href="https://developers.facebook.com/docs/plugins/" data-type="button"></div>
   //so i manually make one.
  <a href="" ng-click='facebookShare();'>Share</a>
 </body>
Run Code Online (Sandbox Code Playgroud)

controller.js

$scope.facebookShare= function(){
   return window.open('http://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href), 'facebook_share', 'height=320, width=640, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
 }
Run Code Online (Sandbox Code Playgroud)

它工作但它没有读取我在上面的html页面上写的元标记,而是从我的索引页面读取 在此输入图像描述

索引页面

<!DOCTYPE html>
<html ng-app='app'>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<noscript>
  <meta http-equiv="refresh" content="7">
    <style type="text/css">
      .pagecontainer {display:none;} 
    </style>
    <div class="pure-u-1 text-center">
    <title>Symsal- Oh no ! We need Javascript to work !</title>
    <h3> Please Enable javascript </h3>
    </div>
</noscript>
  <head>
  <title ng-bind='title'></title>
  // some css file 
  </head>
<body>
<div class='puretype'>
<div ng-controller="MasterCtrl">
<div id="layout">
    <!-- Menu toggle -->
    <a href="" id="menuLink" class=" white menu-link">
        <span></span>
    </a>
<div id='menu' class="pure-u-1">
   <div ng-bind-html="userView"></div> 
</div>
</div>
<div class="pure-g-r">
<div id="feed-container" class='pure-u-1 slide'ng-view=''></div>
</div>
</div>
</div>
// some javascript files 
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

facebook调试器 在此输入图像描述

小智 15

jackypan1989说的是真的,你不得不使用绝对网址.但另一件我非常肯定的事情是,Facebook不会运行你的JavaScript代码,因此这部分

<meta property="og:url" content="/post/{{data.permalink}}">
Run Code Online (Sandbox Code Playgroud)

永远不会被取代

<meta property="og:url" content="/post/the-page-that-was-shared">
Run Code Online (Sandbox Code Playgroud)

您遇到的这个问题也可能会阻止Google抓取您的网页.

我们遇到了同样的问题,我们使用https://prerender.io来解决它.您可以在自己的服务器上使用它,也可以使用其中一种免费/付费服务.