Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json-ld script tag for angularjs2

I am struggling to auto generate the jsonld script in angularjs2, However, I found a solution for angularjs1. Do anyone have solution for this.

like image 692
Arvind Chavhan Avatar asked Jul 05 '16 03:07

Arvind Chavhan


1 Answers

Solution without using a pipe (somewhat clean way)

Use the this.sanitizer.bypassSecurityTrustHtml provided https://angular.io/guide/security#sanitization-and-security-contexts

In the template

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

In the component/directive

  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);
      }
like image 92
Francis Manoj Fernnado Avatar answered Oct 11 '22 02:10

Francis Manoj Fernnado