动态模板" transclusion"在Angular2 [英] Dynamic template "transclusion" in Angular2

查看:184
本文介绍了动态模板" transclusion"在Angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这样的事情:
我有一个名为模型类 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(co​​mpData);
        _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

这篇关于动态模板&QUOT; transclusion&QUOT;在Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆