Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When sharing on facebook, showing angular brackets instead of content

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.

like image 876
Fahad Khan Avatar asked Jan 14 '15 13:01

Fahad Khan


1 Answers

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']; ?>"/>
     }
like image 179
divyenduz Avatar answered Oct 13 '22 18:10

divyenduz