简体   繁体   English

Angular 4:如何使用ng-boostrap api制作动态标签

[英]Angular 4: How to make dynamic tabs using ng-boostrap api

I am attempting to develop a tab view where I could dynamically load tabs during runtime. 我正在尝试开发一个标签视图,我可以在运行时动态加载标签。 I recently started using ngb-tabset from the ng-boostrap, and found it very useful except I can't seem to create tabs dynamically. 我最近开始使用ng-boostrap中的ngb-tabset,发现它非常有用,除了我似乎无法动态创建标签。

Long2know has created almost exactly what I've been trying to implement here. Long2know几乎完全创造了我在这里尝试实现的东西。 The challenge currently is providing the data for this example to work. 目前的挑战是提供此示例的数据。 Ideally, if it was possible to create something like this 理想情况下,如果可以创建这样的东西

<ngb-tabset  (tabChange)="tabChange($event)" [activeId]="activeId">
    <ngb-tab *ngFor="let comp of components; [id]="start" title="tab1">
        <template ngbTabContent >
            <div> *createTab="comp" </div>
        </template>
    </ngb-tab>
</ngb-tabset>

Where "components" would be an array of different components. 其中“组件”是不同组件的数组。 Consequently, createTab would be a directive that used factoryResolver with ViewContainerRef to make component on the fly. 因此,createTab将是一个指令,它使用FactoryResolver和ViewContainerRef来动态创建组件。

If I try something like this I will not get an error, but the tab view will not show at all. 如果我尝试这样的事情,我不会收到错误,但标签视图根本不显示。 After looking at the source code of the ngb-tabset I realized that it uses @ContentChildren to obtain the tab content which doesn't work with creating components dynamically with ViewContainerRef. 在查看ngb-tabset的源代码之后,我意识到它使用@ContentChildren来获取选项卡内容,这些内容不适用于使用ViewContainerRef动态创建组件。

I am just wondering if there is some other way that might be similar to achieve this? 我只是想知道是否有其他方法可能类似于实现这一点? I know there is probably way to accomplish this with the router, however I've invested a lot more time into this approach so it would be preferred, but I'm open to anything. 我知道有可能用路由器来实现这一点,但是我已经投入了更多的时间用于这种方法,所以它会更受欢迎,但我对任何事情都持开放态度。

Try 尝试

<ngb-tabset [destroyOnHide]="false">
...
</ngb-tabset>

This will prevent the elements to be destroyed 这样可以防止元素被破坏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM