javascript - 如何在 bootstrap ngb-tabset 的 Angular 6 中以编程方式加载选项卡

标签 javascript angular twitter-bootstrap typescript angular-bootstrap

我想以编程方式加载选项卡,因为我想使用 Angular 6 typescript 。目前,当我单击选项卡时它会加载。我想通过代码加载。这是我尝试过的示例代码,但它仍然在单击选项卡时加载。

<ngb-tabset #t="ngbTabset" [activeId]="activeIdString" (tabChange)="tabChangeEvent($event)">
  <ngb-tab title="Workforce Plan-Headcount" id="wf1">
    <ng-template ngbTabContent>
      <app-workforce-plan-headcount></app-workforce-plan-headcount>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="Workforce Plan-Cost" id="wf2">
    <ng-template ngbTabContent>
      <app-workforce-plan-cost></app-workforce-plan-cost>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="Current Vacancies" id="wf3">
    <ng-template ngbTabContent>
      <app-current-vacancies></app-current-vacancies>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="Data Entry" id="wf4">
    <ng-template ngbTabContent>
      <app-data-entry></app-data-entry>
    </ng-template>
  </ngb-tab>
</ngb-tabset>

TS文件

activeIdString: string;
    tabChangeEvent(evt: any) {
            this.activeIdString = "wf3";
            console.log(this.activeIdString); 
          }

我尝试了这段代码,它在第一次加载时加载第三个选项卡。然后它不会加载第三个选项卡。有没有以编程方式加载选项卡的最佳方法。

How do you get the selected tab from a bootstrap tabset in Angular4?

https://github.com/ng-bootstrap/ng-bootstrap/issues/1682

http://plnkr.co/edit/IjDOUfoenwYdvW136SPY?p=preview

最佳答案

这对我来说是可行的解决方案,可能对其他人有用

<ngb-tabset #t="ngbTabset" (tabChange)="beforeChange($event)">
  <ngb-tab title="Workforce Plan-Headcount" id="wf1">
    <ng-template ngbTabContent>
      <app-workforce-plan-headcount></app-workforce-plan-headcount>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="Workforce Plan-Cost" id="wf2">
    <ng-template ngbTabContent>
      <app-workforce-plan-cost></app-workforce-plan-cost>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="Current Vacancies" id="wf3">
    <ng-template ngbTabContent>
      <app-current-vacancies></app-current-vacancies>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="Data Entry" id="wf4">
    <ng-template ngbTabContent>
      <app-data-entry></app-data-entry>
    </ng-template>
  </ngb-tab>
</ngb-tabset>

component.ts 文件

public beforeChange($event: NgbTabChangeEvent) {
        if (condition) {
        // prevent from loading
            $event.preventDefault();   
        }
    else{
        this.t.select($event.nextId);
    }
    }

关于javascript - 如何在 bootstrap ngb-tabset 的 Angular 6 中以编程方式加载选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53095895/

相关文章:

javascript - 如何更改 React Native 中的警报按钮颜色?

html - 具有动态大小的列表

javascript - Bootstrap 轮播 - 一张一张显示多张幻灯片时出现问题

javascript - Google Closure 编译器不压缩字符串值?

IE 文本大小 (EG "Largest") 或任何其他可访问性设置(高对比度等)的 Javascript 测试

javascript - 通过 Angular JS 发布多个数组数据

angular - canActivate 在订阅更改时不响应 Angular 2 路由器 AngularFire2

Javascript/两天之间的 Angular 天数(含)

javascript - 异常 : Template parse errors in angular 2. 0 表现不同

twitter-bootstrap - 更改 Bootstrap 输入字段中的占位符日期