使用角度6的动态标签

时间:2020-02-12 14:04:58

标签: typescript angular6 angular5 angular7

我将标签集用于标签。

例如:https://stackblitz.com/edit/ngx-bootstrap-tabs-manual?file=app%2Fapp.module.ts

我需要使用ngx-bootstrap,tabset关闭并动态添加选项卡。

有人可以帮我提供动态标签吗?

1 个答案:

答案 0 :(得分:3)

请在此文件的 app.component.html 中替换以下HTML代码以获取动态标签

<alert type="success">
  <strong>Tabs!</strong> you can enable/disable tab switching with the button here.
</alert>

<label for="switcher">Disable tab switching: <input type="checkbox" [(ngModel)]="disableSwitching"></label>
<p>Tab switching is <strong>{{ disableSwitching ? 'disabled' : 'enabled ' }}</strong>.</p>
<hr>
<tabset #tabset>
  <tab *ngFor="let tab of tabsetData" [heading]="tab.tabtitle" [innerHTML]="tab.tabContent"></tab>
</tabset>

<button (click)='goto(0)'>Go to 1</button>
<button (click)='goto(1)'>Go to 2</button>
<button (click)='goto(2)'>Go to 3</button>

对于 app.component.ts 文件代码

import { Component, ViewChild , ViewChildren , QueryList , AfterViewInit } from '@angular/core';
import { TabsetComponent, TabDirective } from 'ngx-bootstrap/tabs';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit  {
  disableSwitching: boolean;

  @ViewChild('tabset') tabset: TabsetComponent;
  tabsetData = [{
    tabtitle: "First Tab",
    tabContent: "<p>First Tab</p>"
  },{
    tabtitle: "Second Tab",
    tabContent: "<p>Second Tab</p>"
  },{
    tabtitle: "Third Tab",
    tabContent: "<p>Third Tab</p>"
  }];
  ngAfterViewInit(){
    console.log(this.tabset.tabs);
  }

  goto(id){
    this.tabset.tabs[id].active = true;
  }
}

希望这会有所帮助!谢谢。