【问题标题】:AngularJS script tag JSON-LDAngularJS 脚本标签 JSON-LD
【发布时间】:2016-05-21 19:43:21
【问题描述】:

如何在 AngularJS 中使用动态构建的 JSON 对象创建 application/ld+json script 标记。

这就是我需要脚本标签的样子

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Place",
  "geo": {
    "@type": "GeoCoordinates",
    "latitude": "40.75",
    "longitude": "73.98"
  },
  "name": "Empire State Building"
}
</script>

我已经尝试了以下代码,但我无法让它工作:

HTML

<div ng-controller="TestController">
  <script type="application/ld+json">
    {{jsonId|json}}
  </script>
  {{jsonId|json}}
</div>

控制器

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"
  };
}]);

脚本标签内的表达式不执行。 script 标签外的表达式正确执行并显示 JSON

请看jsfiddle

【问题讨论】:

  • 如何判断它是否执行?它只是一个匿名对象...
  • @dandavis json 是一个输出 JSON 对象的过滤器。如果查看源代码,则脚本标签之间没有 JSON 对象。

标签: javascript angularjs json json-ld


【解决方案1】:

喝完咖啡后,我记得有一个$sce 服务和trustAsHtml 函数。

我创建了一个指令,它接受 json 参数以便于重用

请参阅下面的更新和工作代码:

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
  };
}]);

这是脚本输出的图像

请查看更新后的jsfiddle

【讨论】:

  • 如何在 Angularjs 2 中实现同样的效果。
  • @ArvindChavhan 如果脚本标签在您的模板 html 中,它们似乎在 angular2 中被删除,这是一个关于这个问题的问题。stackoverflow.com/questions/38088996/… 这是错误/功能请求的链接@ 987654324@
  • @Tjaart van der Walt 嗨,抱歉我的问题可能听起来很愚蠢,但我有一个延迟加载一些工作的应用程序,我是否必须将所有工作都包含在脚本中才能正常工作?
  • @MohamedMahmoud 如果您需要在 JSON-LD 标记中显示所有作业,那么是的,您需要将它们全部包含在 JSON 输出中。我宁愿在脚本标签中显示最新的作业,或者让脚本标签显示您的页面在初始加载时显示的相同初始数据。这样您就不会滥用带有太多数据的标签。
【解决方案2】:

Tjaart van der Walt 在Google Test Tool 中的回答对我不起作用。它确实适用于真正的爬虫。 所以我找到了另一个“老派”的解决方案:

HTML

<script type="application/ld+json" id="json-ld-music-group"></script>

角度

var schemaOrg = angular.toJson({
    '@context': 'http://schema.org',
    '@type': 'MusicGroup',
    ...
});

angular.element(document).ready(function() {
    var jsonLd = angular.element(document.getElementById('json-ld-music-group'))[0];
    jsonLd.innerHTML = schemaOrg;
});

【讨论】:

  • 我不同意我们在您所说的“真实案例”中使用此解决方案。请点击以下链接search.google.com/structured-data/… 这将启动 googles 结构数据测试工具,并在我的回答中使用示例 jsfiddle。您将看到在右侧检测到地方。忽略错误,因为那是 jsfiddles 网站数据而不是示例 jsfiddle。你确定你没有尝试测试本地主机网站吗?
  • 你是对的。毕竟它奏效了。谷歌处理结果延迟了 5 天。它只是在我的谷歌测试工具中不起作用。我编辑了我之前的评论。
猜你喜欢
  • 1970-01-01
  • 2021-10-15
  • 2018-03-30
  • 2018-02-03
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多