【发布时间】:2018-04-25 08:07:24
【问题描述】:
我正在尝试使用打字稿添加带有 id 的 div,但它没有分配它。我只得到 div
app.component.html
<div class="one" [innerHtml]="htmlToAdd">
</div>
app.component.ts
import { Component } from '@angular/core';
import { Http } from '@angular/http';
import * as shape from 'd3-shape';
import { ElementRef } from '@angular/core';
declare var $: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
ngOnInit() {
this.addDiv()
}
addDiv(){
this.htmlToAdd = '<div class="two" id="myId">my div</div>';
}
}
【问题讨论】:
-
你可能不得不绕过 Angular 的安全机制。 check this
-
@ochs.tobi 请删除您的评论。这是错误的诱导和完全错误的。
-
即使使用 domsanitizer 也无法通过 innerHTML 以角度添加动态 HTML。它只是将 HTML 呈现为标记。你需要动态组件加载之类的东西
标签: angular