1

I am generating JSON-LD in a script tag in the body section using this solution in AngularJS 1.4.8. The directive is defined as

myApp.directive('jsonld', ['$filter', '$sce', function($filter, $sce) {
  return {
    restrict: 'E',
    template: function() {
      return '<script type="application/ld+json" ng-bind-html="onGetJson()"></script>';
    },
    scope: {
      json: '=json'
    },
    link: function(scope, element, attrs) {
      scope.onGetJson = function() {
        return $sce.trustAsHtml($filter('json')(scope.json));
      }
    },
    replace: true
  };
}]);

And jsonId is defined in the controller

myApp.controller('MyWorkCtrl',['$scope', '$routeParams', '$http', function($scope, $routeParams, $http){
    $http.get('/works/' + $routeParams.workId + '.json').success(function(data) {   
        $scope.work = data;
        $scope.jsonId = {
            "@context": "http://schema.org",
            "@type": "Book",
            "creator": data['authors'],
            "name": data['title'],
            "isbn": data['isbn'],
            "bookFormat": data['page_size'],
            "numberOfPages": data['pages'],
            "translator": data['translators'],
            "image": data['image']
        };
    });    
}]);

Then it's just a matter of placing the directive

  <jsonld data-json="jsonId"></jsonld>

It works fine, except that the script tag with Json-LD is generated in the body. How could I move it to head section as Google recommends?

3
  • Where does Google recommend this? Having a JSON-LD script in the body should be perfectly fine. Commented Apr 20, 2017 at 23:49
  • "Markup is placed inside a script tag in the head of the HTML page." developers.google.com/search/docs/guides/intro-structured-data Commented Apr 21, 2017 at 12:05
  • I see, thanks. This is most likely just a formulation that is not as precise as it could be. Commented Apr 21, 2017 at 15:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.