angularjs2的Json-ld脚本标记

Arv*_*han 5 json-ld angular

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

Fra*_*ado 3

不使用管道的解决方案(有点干净的方式)

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

在模板中

<div [innerHtml]="jsonLDString"></div>
Run Code Online (Sandbox Code Playgroud)

在组件/指令中

  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);
      }
Run Code Online (Sandbox Code Playgroud)