Angular 如何防止ngx引导上的选项卡选择

Angular 如何防止ngx引导上的选项卡选择,angular,twitter-bootstrap,tabs,ngx-bootstrap,Angular,Twitter Bootstrap,Tabs,Ngx Bootstrap,我有两个类似于bellow的ngx引导选项卡,我想通过编程停止选项卡功能: <tabset> <tab heading="First">Tab 1 content</tab> <tab heading="Second">Tab 2 content</tab> </tabset> <label>Edit in progress: </label> <input type="che

我有两个类似于bellow的ngx引导选项卡,我想通过编程停止选项卡功能:

<tabset>
    <tab heading="First">Tab 1 content</tab>
    <tab heading="Second">Tab 2 content</tab>
</tabset>

<label>Edit in progress: </label>
<input type="checkbox" [(ngModel)]="isTabSwitchEnabled">
this.second.active=true
不会切换到新选项卡,只会将其标记为活动。如果我尝试使用
this.tabset.tabs[index].active=true,情况也一样

有没有办法实现这个功能?启用和禁用切换选项卡?理想情况下,也将其绑定到路由器上(但我确实需要编程访问)。

这里的问题是:

@ViewChild('first') first: TabDirective;
不指向
TabDirective
,只指向native
ElementRef
,因此
this.second.active=true将不会激活
TabDirective

只需使用
this.tabset.tabs[index].active=true


以下是一个有效的stackblitz示例-

这里的问题是,
select
事件在tabset将所有其他选项卡的
active
属性设置为
false
之前发出,因此需要将选择选项卡的代码包装为
setTimeout
或类似的内容。这将在选项卡集的所有内部操作之后设置活动选项卡

编辑:正如OP所说,当前(ngx-bootstrap@2.0.2)解决方法是在选项卡集组件上找到匹配的元素并激活:

confirmTabSwitch($event) {
  if (this.disableSwitching) {
    const confirm = window.confirm('Discard changes and switch tab?');
    if (confirm) {
      this.disableSwitching = false;
      const liArr = Array.from(this.tabsetEl.nativeElement.querySelectorAll('ul li'));
      let tabIndex;
      liArr.forEach((li, i) => {
        if(li.contains($event.target)) {
          tabIndex = i;
        }
      });
      setTimeout(() => this.tabset.tabs[tabIndex].active = true);
    }
  }
}

谢谢知道这一点有些有用,但我对这个演示还有一点问题:单击选项卡2。然后选中复选框。然后再次单击选项卡1,并用“确定”确认。它不会切换回1。下面的示例中有一个硬代码,只是为了演示。一个动态的版本看起来有点像这样。这个实现很混乱,但作为一个解决方案,它是可以的。我们将在未来的版本中提供一个API来防止选项卡选择。谢谢,但我没有使用ElementRef,您可以在问题和链接的Stackblitz中检查代码。此外,您的工作演示链接也不起作用。是的,我知道,但如果您检查一下,它会返回
ElementRef
而不是
TabDirective
,因为该活动代码的部分不起作用。
confirmTabSwitch($event) {
  if (this.disableSwitching) {
    const confirm = window.confirm('Discard changes and switch tab?');
    if (confirm) {
      this.disableSwitching = false;
      const liArr = Array.from(this.tabsetEl.nativeElement.querySelectorAll('ul li'));
      let tabIndex;
      liArr.forEach((li, i) => {
        if(li.contains($event.target)) {
          tabIndex = i;
        }
      });
      setTimeout(() => this.tabset.tabs[tabIndex].active = true);
    }
  }
}