angular - angularjs2 的 Json-ld 脚本标签

标签 angular json-ld

我正在努力在 angularjs2 中自动生成 jsonld 脚本,但是,我找到了 angularjs1 的解决方案。 有没有人对此有解决方案。

最佳答案

不使用管道的解决方案(比较干净的方式)

使用提供的this.sanitizer.bypassSecurityTrustHtml https://angular.io/guide/security#sanitization-and-security-contexts

在模板中

<div [innerHtml]="jsonLDString"></div>

在组件/指令中

  private jsonld: any;
  public jsonLDString: any;

   private setJsonldData() {
        this.jsonld = {
          '@context': 'http://schema.org/',
          '@type': 'Service',
          'name': this.providerName,
          'description': this.providerDescription,
          'aggregateRating': {
            '@type': 'AggregateRating',
            'ratingValue': '3',
            'bestRating': '5',
            'ratingCount': '3'
          },
          'url' : this.providerUrl
        };
        this.jsonLDString = '<script type="application/ld+json">' + JSON.stringify(this.jsonld) + '</script>';
        this.jsonLDString  = this.sanitizer.bypassSecurityTrustHtml(this.jsonLDString);
      }

关于angular - angularjs2 的 Json-ld 脚本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194903/

相关文章:

Angular-Meteor 更新到 1.6.1.1 typescript 编译器缺少声明

Angular 4 Routing 不适用于实时网络应用程序

angular - 尝试在 Firebase 中托管时出现 NullInjectorError : StaticInjectorError on NgxSpinnerService; NullInjectorError: No provider for t!

schema.org - 如何为我的组织指定父组织?

javascript - 如何在 Angular typescript 中使用 Timetable.js?

扩展面板之间的 Angular Material 拖放项目不起作用

seo - 如何在不获取 "application/ld+json"的情况下在 Google 搜索 "Missing product ID (optional)"脚本中指定产品 ID

seo - 在 JSON-LD 中创建产品数组

seo - 如何在 Review Rich Snippet 中添加评论来源(如链接)?

schema.org - 通过 JSON-LD schema.org 连接多个组织和网站