JSON-LD output with AnugularJS for parsing structured data

I searched the web for a way to create and output a JSON-LD object using AngularJS, but with no luck.

What I'm trying to achieve is to add structured data to my SPA, as described for the events here:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Event",
  "name": "Example Band goes to San Francisco",
  "startDate" : "2013-09-14T21:30",
  "url" : "http://example.com/tourdates.html",
  "location" : {
    "@type" : "Place",
    "sameAs" : "http://www.hi-dive.com",
    "name" : "The Hi-Dive",
    "address" : "7 S. Broadway, Denver, CO 80209"
  }
}
</script>

https://developers.google.com/structured-data/rich-snippets/events

The easiest way to do this is to create a JSON-LD object and output it to a script tag. But, as far as I know, it is impossible / good practice to access the values ​​of the area inside the script tag as follows:

<script type="application/ld+json">
{{jsonLdObject}} 
</script>

Can someone help me with a better approach to this, and is it good to create a JSON-LD object as a regular JSON object?

+4
1

, : fooobar.com/questions/417617/...

HTML:

<div ng-controller="TestController">
  <jsonld data-json="jsonId"></jsonld>
</div>

JavaScript:

var myApp = angular.module('application', []);

myApp.controller('TestController', ['$scope', function($scope) {
  $scope.jsonId = {
    "@context": "http://schema.org",
    "@type": "Place",
    "geo": {
      "@type": "GeoCoordinates",
      "latitude": "40.75",
      "longitude": "73.98"
    },
    "name": "Empire State Building"
  };
}]).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
  };
}]);
+3

Source: https://habr.com/ru/post/1623049/


All Articles