【发布时间】:2016-12-20 12:13:50
【问题描述】:
我正在尝试使用以下代码实现动态组件-
createWidget(template: string) {
const html = template;
if (!html) return;
if (this.cmpRef) {
this.cmpRef.destroy();
}
const compMetadata = new Component({
selector: 'dynamic-html',
template: html
});
this.createComponentFactory( this.compiler, compMetadata )
.then(factory => {
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
console.log("Injector: " + injector);
this.vcRef.createComponent(factory, 0, injector, []);
console.log( "Create Widget End.." );
});
}
createComponentFactory( compiler: Compiler, metadata: Component) {
@Component( metadata )
class DynamicComponent { };
@NgModule({
imports: [ CommonModule, FormsModule ],
declarations: [ DynamicComponent ]
})
class DynamicHtmlModule { }
return compiler.compileModuleAndAllComponentsAsync( DynamicHtmlModule )
.then(( moduleWithComponentFactory: ModuleWithComponentFactories<any> ) => {
return moduleWithComponentFactory.componentFactories.find( x => x.componentType === DynamicComponent );
});
}
当我调用 createWidget 方法时,代码运行良好-
this.createWidget('Angular2');
但问题是,当我使用 for 循环多次调用 createWidget 时,生成的模板是反向排序的。请看我的插件https://plnkr.co/edit/hqKYsGlyuk78tg61zjfd?p=preview
在这个 plunker 示例中,Angular2 显示在 Angular1 之前。
【问题讨论】:
标签: angular