Ionic DomSanitizer给出Uncaught TypeError:无法读取null的属性'offsetHeight'

时间:2017-11-15 10:32:49

标签: angular typescript ionic-framework html-sanitizing

正如许多其他人一样,我正试图从服务器检索HTML页面,以便在IONIC应用程序上显示它。

这是模板/ html:

<ion-content padding [innerHtml]="HTMLcontent">
</ion-content>

这是模块:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http } from '@angular/http';
import { DomSanitizer } from "@angular/platform-browser";

export class RandomPage {
  HTMLcontent;
  constructor(public navCtrl: NavController, public navParams: NavParams, private http:Http, private sanitizer: DomSanitizer) {}
  ionViewDidLoad() {
    this.http.get("SAFE_URL_HERE").subscribe(html=>{
      this.HTMLcontent = this.sanitizer.bypassSecurityTrustHtml(html['_body']);
    });
  }
}

现在,问题在于,每当我尝试访问此页面时,都会收到此错误:

Uncaught TypeError: Cannot read property 'offsetHeight' of null
    at Content.getContentDimensions (content.js:529)
    at Content._readDimensions (content.js:693)
    at content.js:220
    at SafeSubscriber.schedulerFn [as _next] (core.js:4235)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
    at SafeSubscriber.next (Subscriber.js:185)
    at Subscriber._next (Subscriber.js:125)
    at Subscriber.next (Subscriber.js:89)
    at EventEmitter.Subject.next (Subject.js:55)
    at EventEmitter.emit (core.js:4203)

我现在找到的唯一解决方法是使用这样的模板:

<ion-content padding>{{HTMLcontent}}</ion-content>

但它会输出文本而不是HTML,所以不行

1 个答案:

答案 0 :(得分:1)

尝试在ion-content标签内使用div

<ion-content padding>
<div [innerHtml]="HTMLcontent"></div>
</ion-content>