Angular 插入 HTML 字符串要先通过 sanitizer 处理。
html
<div class="svg" [innerHTML]="svg"></div>
js
import { Component, OnInit } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
Component({
@: 'app-home',
selector: './home.component.html',
templateUrl: ['./home.component.css']
styleUrls
})export class HomeComponent implements OnInit {
:SafeHtml;
svg
constructor(private sanitizer: DomSanitizer) { }
ngOnInit() {
this.svg = this.sanitizer.bypassSecurityTrustHtml("‹‹ SVG CONTENT ››");
}
}