我正在尝试将文本文件显示为 html。我正在使用 ionic 。 我正在发送一个 html 格式的响应,但在一个文本文件中发送到配置文件页面。它在 .ts 页面的变量名中。
@Component({
selector: 'page-profile',
templateUrl: 'profile.html',
})
export class ProfilePage{
name: any;
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.name = navParams.get('name');
现在我尝试使用 html 格式显示它
<div [innerHtml]="name">{{name}}</div>
但我得到的是文本格式,而不是 html。如何以html格式显示?
最佳答案
您需要使用 DomSanitizer绕过您提供的 html(bypassSecurityTrustHtml)。
constructor(public navCtrl: NavController,
public navParams: NavParams,
private sanitizer: DomSanitizer) {
this.name = sanitizer.bypassSecurityTrustHtml(navParams.get('name'));
}
在这里阅读更多:https://angular.io/guide/security#bypass-security-apis
关于html - 将带有 html 标签的文本显示为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50964849/