Angular2 choose template dynamically according to service response

前端 未结 1 1386
孤城傲影
孤城傲影 2021-02-14 22:21

I have a component called "Node" which should display a single post, but I have more than a post type and each type has its own view (layout).

So this componen

1条回答
  •  广开言路
    2021-02-14 22:35

    If you have a limited set of possible templates then *ngIf or *ngSwitch is a good approach. It allows you to use [] and () bindings between parent and child.

    If you have a set of templates that are statically unknown and for example provided by a parameter then *ngIf doesn't work. Then ViewContainerRef.createComponent() (replaces DynamicComponentLoader) is the right approach. For an example see Angular 2 dynamic tabs with user-click chosen components

    0 讨论(0)
提交回复
热议问题