6

AngularJS rendering the content fine into title and meta tags, but when I share it with facebook or google, in the popup window it shows angular {{ }} there. enter image description here

Code:

<div ng-controller="MyCtrl">
 <title>{{mDetails.display1}} - Subtitle</title> 
<meta name="description" content="{{mDetails.display1}} - {{mDetails.address1}} , {{mDetails.city}}, {{mDetails.state}}, {{mDetails.country}}">
.
.

Note: I am already using ng-cloak.

Thanks for help.

Fahad Khan
  • 1,635
  • 4
  • 23
  • 37

2 Answers2

3

Two ways, as mentioned on another question :- og meta tags, social buttons and angularjs

Method 1 :-

This can't be done using javascript. Some people think that Facebook is reading what's currently on the page. It's not. It makes a separate request to your server using the same url (from window.location.href) using it's Scraper, and the Facebook Scraper does not run javascript. That's why you get {{page_title}} when clicking on something like a Facebook share button. Your content will have to be generated by the server so when Facebook goes to hit the url it gets the content it needs up front without the need for javascript. You can tackle the server side rendering in a fews ways.

You can allow your server side technology to render the content.
You can use the PhantomJS approach https://github.com/steeve/angular-seo.

Method 2 :-

There's also a possibility that you can re-render Facebook widgets. Use their parse method:

FB.XFBML.parse();

after your angular stuff has completed. It's not working for my share button (yet!!), but I tested it on likes, and it's cool. Basically it re-scans the DOM and renders the Facebook widgets. You can also pass it a single element, something like this directive:

'use strict';    
angular.module('ngApp')
.directive("fbLike", function($rootScope) {
    return function (scope, iElement, iAttrs) {
        if (FB && scope.$last) {
           FB.XFBML.parse(iElement[0]);
        }
    };
});

This snippet would rescan the DOM for html5 facebook fb-like widgets when creating the last element in angular repeater.


Another accepted answer in the same context :- https://stackoverflow.com/a/24086652/1366216

Edit:

I implemented json on server side just for the meta tags, however its an overhead because for on page data, there is still ajax call.

$mid=$_GET['id'];
    $mJSON = file_get_contents($homeurl."/json/getdetail.php?mid=".$mid);
    $mObject = json_decode($mJSON, true);
    if ($mObject['ID'] != undefined && $mObject['ID'] != '') {
      <meta property="og:title" content="<?php echo $mObject['display1'];?>"/>
      <meta property="og:description" content="<?php echo .$mObject['display2']; ?>"/>
     }
Community
  • 1
  • 1
divyenduz
  • 2,037
  • 19
  • 38
  • Trying 1st method as Michael discussed here: http://www.michaelbromley.co.uk/blog/171/enable-rich-social-sharing-in-your-angularjs-app – Fahad Khan Jan 15 '15 at 06:31
  • Not exactly, instead I just used PHP and echo the tags, but still looking for a better way to check ON PAGE if its crawlers or not, instead of configuring apache. – Fahad Khan Jan 16 '15 at 06:36
  • Ok, if I find across the original way you wanted to do it. I will update the answer :) – divyenduz Jan 16 '15 at 07:06
  • I'm hitting this a few years later, but having the same issue. Yes, I still have an AngularJS or two running. When I link them on FB, I get this issue - which makes it look horrible on FB. I'm not quite understanding where/how to implement what you're saying here in Method 2. Do you have an example I could look at? – Reverend Bubbles May 20 '20 at 21:28
0

A possibility that worked for me is to have a "fallback" for FB description (so it would be a generic description that shows when angular hasn't loaded). I achieved this using two ng-if.

<meta ng-if="mDetails.display1" property="og:title" content="{ mDetails.display1 + "- Subtitle"}}">
<meta ng-if="!mDetails.display1" property="og:title" content="A generic title">

Whenever there are two meta descriptions with the same title, FB will take the last one. And after rendering, googlebot will only see the non-default one.

Joe82
  • 1,357
  • 2
  • 24
  • 40