【问题标题】:Angular2 innerHTML removes property, help needed to use DomSanitizerAngular2 innerHTML 删除属性,使用 DomSanitizer 需要帮助
【发布时间】:2017-11-17 07:20:36
【问题描述】:

我只需要用id"main-wrapper" 将HTML 注入到一个div 中,所以在我的component.ts 中我正在使用此代码

    import { Component, OnInit, ElementRef,Renderer2,Inject } from '@angular/core';
    import { Pipe } from '@angular/core';
    import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

    @Component({
      selector: 'app-editsection',
      templateUrl: './editsection.component.html',
      styleUrls: ['./editsection.component.css']
    })

    export class EditsectionComponent implements OnInit {
    ...//some code

    constructor(
        @Inject(DOCUMENT) private document: any,
        private route: ActivatedRoute,
      private elRef: ElementRef,
      private el: ElementRef,
      private _sanitizer:DomSanitizer
      ) { }

    ngOnInit() {
    var strHTML = '<p>abc<p>';
     this.document.getElementById("main-wrapper").innerHTML += this._sanitizer.bypassSecurityTrustHtml(strHTML);

    ...
    }
    }

当我运行代码时它说: SafeValue 必须使用 [property]=binding: abc

(见http://g.co/ng/security#xss

为什么我需要实现这个 - 因为当我注入 innerHTML 时,我失去了一个属性 contenteditable="true"

在应用 innerHTML 之前,我的代码如下所示:

<h1 _ngcontent-c2 contenteditable="true">Hii<h2>

应用innerHTML后变成:

<h1 _ngcontent-c2>Hii<h2>

请帮我解决问题

【问题讨论】:

    标签: angular typescript dom angular-dom-sanitizer


    【解决方案1】:

    Angular 背后的整个方法都基于 http://angularjs.blogspot.com.au/2016/04/5-rookie-mistakes-to-avoid-with-angular.html 中建议的通过脚本(例如您拥有的)减少 DOM 操作。

    在极少数情况下需要直接操作 DOM。 Angular 2 提供了一组强大的高级 API,例如可以使用的查询。利用这些 API 具有一些明显的优势

    ...

    当您手动操作 DOM 时,您会错失这些优势,并最终编写出表达能力较差的代码。


    所以不要使用这个:this.document.getElementById("main-wrapper").innerHTML +=

    您应该使用 Angular 中固有的模板引擎/结构指令,例如 *ngFor *ngIf

    // .html
    <div class="main-wrapper"><p *ngIf="showabc">abc</p></div>
    // .ts
    var showabc: Boolean = true;
    

    根据您的评论:

    您正在从本地存储加载一堆 html。在这种情况下,您将不得不操作 DOM。理想情况下,我建议出于性能目的重新配置此架构,如上所述。

    1st,将 html 加载到 typescript...

    public possibleHTML: Array; 
    constructor(private sanitizer: DomSanitizer){}
    ngOnInit(){  
       this.possibleHTML = loadContentFromLocalStorage();
       this.possibleHTML = this.possibleHTML.map(value => this.sanitizer.bypassSecurityTrustHtml(value));
    }
    

    第二次插入html。

    <div class="main-wrapper">
        <content *ngIf="possibleHTML">
           <div *ngFor="let html of possibleHTML">
               <div *ngIf="html.makevisible" [innerHtml]="html"></div>
           </div>
        </content>
    </div>
    

    缺点: css 样式不会生效,除非它被定义为全局样式表 styles.css 而不是 editsection.component.css

    【讨论】:

    • 我只是用了一个例子&lt;p&gt;abc&lt;p&gt;。我将 HTML 代码保存在本地存储中,并取决于将要注入的用户选择。那么我该怎么做呢?我正在尝试这样的事情this.document.getElementById("main-wrapper").innerHTML += this.section_desc; //injecting HTML HERE
    • 如果我像这样添加&lt;div class="main-wrapper"&gt;{{section_desc}}&lt;/div&gt;,那么 HTML 不会生效,请看这里 - dropbox.com/s/ezw90ib6ncoxo4d/…,我该怎么办?
    • @JessicaStorm 现在为您提供一个示例 - 请 5 分钟。
    • 我这样称呼我的 css - ` ` ,它会工作吗?
    • 我正在使用这样的 - var strHTML = '&lt;p&gt;abc&lt;p&gt;'; this.possibleHTML = strHTML; 和 html &lt;content *ngIf="possibleHTML"&gt; &lt;div *ngIf="possibleHTML.makevisible" [innerHtml]="possibleHTML"&gt;&lt;/div&gt; &lt;/content&gt; 但它没有显示 html 内容
    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多