我从WordPress网站上获取一些数据。哪个需要在应用中显示。所有HTML都在加载。但图像没有加载。我在很多Android设备上测试过它。这是我的代码:
HTML
<div id="post-details" [class]="'post-details-'+post.id" [innerHtml]="post.post_content | safe"></div>
SafePipe.ts
import { Pipe } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';
@Pipe({
name: 'safe'
})
export class SafePipe {
constructor(protected _sanitizer: DomSanitizer) {
}
public transform(value: string, type: string = 'html'): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
switch (type) {
case 'html': return this._sanitizer.bypassSecurityTrustHtml(value);
case 'style': return this._sanitizer.bypassSecurityTrustStyle(value);
case 'script': return this._sanitizer.bypassSecurityTrustScript(value);
case 'url': return this._sanitizer.bypassSecurityTrustUrl(value);
case 'resourceUrl': return this._sanitizer.bypassSecurityTrustResourceUrl(value);
default: throw new Error(`Invalid safe type specified: ${type}`);
}
}
}