angular标签页动态渲染组件

业务需求:需要实现动态页签(页签功能已实现),并在标签内容页渲染组件。

html代码

<p-tabView>

    <p-tabPanel *ngFor="let item of tabPanels" header="{{item.title}}">

        <!-- <jhi-tabpage1></jhi-tabpage1> -->

        <!-- 直接写组件<jhi-tabpage1>可以正常渲染,但使用下面的动态{{}}就只渲染标签字符串 -->

        {{content}}

    </p-tabPanel>

</p-tabView>

Anguar代码

ngOnInit() {

    this.tabPanels = [

        {title: '页签1', content: '<jhi-tabpage1></jhi-tabpage1>'},

        {title: '页签2', content: '<jhi-tabpage2></jhi-tabpage2>'}

    ];

}

效果如下图:

https://img2.mukewang.com/5c457a5d0001554b08000171.jpg

问题

  1. 这种情况下,请问如何动态渲染tab页的组件,有如思路方向?

  2. ComponentFactoryResolver? DynamicComponentLoader? 建议如何实现此情况?

感谢各位大神的关注!


慕姐8265434
浏览 369回答 1
1回答

一只斗牛犬

<p-tabView>&nbsp; &nbsp; <p-tabPanel *ngFor="let item of tabPanels" header="{{item.title}}">&nbsp; &nbsp; &nbsp; &nbsp; <!-- <jhi-tabpage1></jhi-tabpage1> -->&nbsp; &nbsp; &nbsp; &nbsp; <!-- 直接写组件<jhi-tabpage1>可以正常渲染,但使用下面的动态{{}}就只渲染标签字符串 -->&nbsp; &nbsp; &nbsp; &nbsp; <div [innerHtml]="item.content"></div>&nbsp; &nbsp; &nbsp; &nbsp; {{content}}&nbsp; &nbsp; </p-tabPanel></p-tabView>这样试试
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript