如何以编程方式在引导ngb-tabset的角度6中加载选项卡

时间:2018-11-01 05:58:15

标签: javascript angular twitter-bootstrap typescript angular-bootstrap

我想像使用棱角6打字稿一样按比例加载标签。当前,当我单击选项卡时它将加载。我,想通过代码加载。这是我尝试的示例代码,但仍然可以在单击选项卡时加载。

<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

1 个答案:

答案 0 :(得分:0)

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

<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);
    }
    }