angular2展示包含html標簽的內容


angular2采用{{變量}}的方式展示數據,但字符串中包含html代碼,會被自動過濾掉。

采用<span [innerHTML]="b"></span>這種方式可以直接將html代碼展示出來。

 

但這樣寫又會存在一個新問題:展示的html標簽中,style的屬性會被過濾掉。

坑~~~

 

解決方法:使用ng2服務DomSanitizer中的bypassSecurityTrustHtml 方法

import { Component, OnInit } from '@angular/core';  
import { DomSanitizer } from '@angular/platform-browser';  
  
@Component({  
    selector: 'my-zhizaoZixunDetail',  
    templateUrl: './zhizaoZixunDetail.component.html',  
    styleUrls: [ './zhizaoZixunDetail.component.css' ],  
    providers: [ZhizaoZixunDetailService]  
  })  
export class ZhizaoZixunDetailComponent implements OnInit {  
    constructor(private activatedRoute: ActivatedRoute,  
        private domSanitizer: DomSanitizer,  
        private zhizaoZixunDetailService: ZhizaoZixunDetailService) {};  
    ngOnInit(): void {  
        var results = this.zhizaoZixunDetailService.getData(this.zhizaoZixun);  
        results.then((response) => {  
            if(response!=null) {  
                this.detail=response;   
                this.detail["wenzhNeir"]=  
            this.domSanitizer.bypassSecurityTrustHtml(this.detail["wenzhNeir"]);  
            }  
         }  
    }  
}    

  

用domSanitizer.bypassSecurityTrustHtml轉換一下就可以解決了。

 

參考:http://www.jianshu.com/p/ef008e9c07de


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM