请告诉我们如何在 Angular2/4 中执行以下操作:
成分SomeComponent
来自@Input
以下 html 作为字符串:
`<am-input
[placeholder]="'Placeholder'"
[disabled]="true">
</am-input>`
How can SomeComponent
仅从该字符串在其内部创建一个组件?
thanks
唯一的选择是使用 JIT 编译器并在获得时进行编译。阅读以下是您需要了解的有关 Angular 动态组件的知识, 具体来说Creating components on the fly
部分。它详细解释了该过程。要点如下:
class SomeComponent {
@Input inputTpl;
constructor(private _compiler: Compiler,
private _injector: Injector,
private _m: NgModuleRef<any>) {
}
ngOnChanges() {
const tmpCmp = Component({template: inputTpl})(class {});
const tmpModule = NgModule({declarations: [tmpCmp]})(class {});
this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = f.create(this._injector, [], null, this._m);
cmpRef.instance.name = 'dynamic';
this.vc.insert(cmpRef.hostView);
})
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)