我需要创建一个生成任意HTML的指令。指令应该生成基于外部数据的HTML代码。
示例:
@Directive({ selector: '[layout]' })
export class Layout {
constructor(
private templateRef: TemplateRef,
private viewContainer: ViewContainerRef){
}
@Input() set layout(value) {
if (value) {
// Only for example
var div = document.createElement('div');
div.innerHTML = '<header>This is {{value}} header</header>';
// ??? How to put the div element to the View
var embeddedViewRef = this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
}
如何正确创建指令的HTML代码?