网站搜索

angular - Ionic 2 LoadingController 包含自定义内容


我想在我的 Ionic 2 项目的 LoadingController 的 content 选项中使用自定义组件,但当加载出现时,浏览器控制台给我显示消息:

WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).

并且组件没有显示,在测试时我意识到使用任何不是标准 HTML 标签的标签都会抛出此错误,我如何使用 Angular 2 安全性来绕过这个错误?我想要的是:

    this.loadingController.create({
        spinner: 'hide',
        content: `
        <div>
            <some-component></some-component>
            <h3>a message...</h3>
        </div>`
    })

答案#1

实际上,根据文档:你不能那样做

内容 ----> 字符串 ----> 加载指示器的 HTML 内容。

所以你必须像这样传递普通的 string HTML 内容:

content: `
      <div class="my-spinner">
        <div class="my-spinner-box"></div>
      </div>`,

答案#2

这对我有效:

//----------imports----------
import { DomSanitizer } from '@angular/platform-browser';

//----------variables----------
safeHtml: any;

//----------constructor----------
constructor(public loadingCtrl: LoadingController, private sanitizer: DomSanitizer)

//----------your function----------
let svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox...whatever..</svg>';
// you can also add text BEFORE sanitizing
let html = svg + '<div class="loadingText">' + this.translate.instant('common.Loading') + '</div>';

this.safeHtml = this.sanitizer.bypassSecurityTrustHtml(html);

var loading = this.loadingCtrl.create({
    spinner: 'hide',
    content: this.safeHtml
});
loading.present();