我正在努力在 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/