我有一个div,我必须添加一些变量的HTML代码,使用Angular2。
import {Component,Input,ViewChild,ElementRef,OnInit} from 'angular2/core';
@Component({
selector: 'my-view',
template: `
<div #myDiv>{{str}}</div>
`,
styleUrls:['src/css/thedata.css']
})
export class AngularComponent{
private str = "<div class='EachWidget FL'><div class='WidgetDataParent'><div class='widgetTitleBar'></div></div></div>";
}
我必须在名为myDiv的div中添加str字符串作为html。
答案 0 :(得分:3)
您可以像这样绑定innerHTML
属性:
import {Component,Input,ViewChild,ElementRef,OnInit} from 'angular2/core';
@Component({
selector: 'my-view',
template: `
<div [innerHTML]="str" #myDiv></div>
`,
styleUrls:['src/css/thedata.css']
})
export class AngularComponent{
private str = "<div class='EachWidget FL'><div class='WidgetDataParent'><div class='widgetTitleBar'></div></div></div>";
}