问题描述
我想实现这样的事情:
我有一个名为模型类 ObjectTypeA
, ObjectTypeB
和 ObjectTypeC
。
还有一个工厂 ComponentFactory
,它基于传入对象的类型将创建不同的组件:
ComponentFactory.ts
导出接口ComponentFactoryInterface { 静态CreateComponent(OBJ:CommonSuperObject)}@Injectable()
出口类ComponentFactory { 公共静态CreateComponent(OBJ:CommonSuperObject){ 如果(OBJ的instanceof ObjectTypeA){
返回ObjectComponentA()
}否则如果(OBJ的instanceof ObjectTypeB){
返回ObjectComponentB()
} }
}
在模板我愿做这样的事情:
main_template.html
<成分>
<成分* ngFor =#OBJ中的对象>
<! - 在这里插入自定义组件模板 - >
< /成分>
< /成分>
我要如何插入相关组件动态?
我可以做这样的事情(不是我的preferred做它的方式):
<成分>
<! - 遍历组件模型 - >
<定制组件-A * ngIf =模型[I]的instanceof ObjectTypeA>
< /自定义组件的A>
<定制组件-B * ngIf =模型[I]的instanceof ObjectTypeB>
< /自定义组件的B>
< /成分>
但这似乎真的错了我在许多层面上(我会,如果我添加其他组件类型修改此code和工厂code)。
修改 - 工作实例
构造函数(私人_contentService:contentService的,私人_dcl:DynamicComponentLoader,componentFactory:ComponentFactory,elementRef:ElementRef){ this.someArray = _contentService.someArrayData;
this.someArray.forEach(compData => {
让组件= componentFactory.createComponent(compData);
_dcl.loadIntoLocation(组分,elementRef,'componentContainer')。然后(功能(EL){
el.instance.compData = compData;
});
});}
您可以使用 ngSwitch
(类似于你自己的解决方法,但更简洁)或 DynamicComponentLoader
另请参见
I am trying to achieve something like this:I have a model class called ObjectTypeA
, ObjectTypeB
and ObjectTypeC
.There is also a factory ComponentFactory
, which based on the type of object passed in will create different components:
ComponentFactory.ts
export interface ComponentFactoryInterface {
static CreateComponent(obj: CommonSuperObject)
}
@Injectable()
export class ComponentFactory {
public static CreateComponent(obj: CommonSuperObject){
if (obj instanceof ObjectTypeA){
return ObjectComponentA()
}else if (obj instanceof ObjectTypeB){
return ObjectComponentB()
}
}
}
In the template I would like to do something like this:
main_template.html
<components>
<component *ngFor="#obj in objects">
<!-- insert custom component template here -->
</component>
</components>
How do I insert the associated components dynamically ?
I could do something like this (not my preferred way of doing it):
<components>
<!-- loop over component models -->
<custom-component-a *ngIf="models[i] instanceof ObjectTypeA">
</custom-component-a>
<custom-component-b *ngIf="models[i] instanceof ObjectTypeB">
</custom-component-b>
</components>
But this is seems really wrong to me on many levels (I would have to modify this code and the factory code if I add another component type).
Edit - Working Example
constructor(private _contentService: ContentService, private _dcl: DynamicComponentLoader, componentFactory: ComponentFactory, elementRef: ElementRef){
this.someArray = _contentService.someArrayData;
this.someArray.forEach(compData => {
let component = componentFactory.createComponent(compData);
_dcl.loadIntoLocation(component, elementRef, 'componentContainer').then(function(el){
el.instance.compData = compData;
});
});
}
You can use ngSwitch
(similar to your own workaround but more concise) or DynamicComponentLoader
See also
- https://angular.io/docs/ts/latest/api/core/DynamicComponentLoader-class.html
- How to use angular2 DynamicComponentLoader in ES6?
这篇关于动态模板&QUOT; transclusion&QUOT;在Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!