Select active md-tab md-tab-group inside md-dialog

I need to open an md dialog containing an md-tab group with two tabs. The md dialog box can be opened with two buttons that should open the corresponding tab. The template from which the md-dialog opens:

<button md-button class="" (click)="openDialog(1)">open tab 1</button>
<button md-button class="" (click)="openDialog(2)">open tab 2</button>

Component .ts:

    openDialog(type) {

      var data: any = {};
      data.type = type;

      let dialogRef = this.dialog.open(TwoTabDialog, {height: 'auto', width: '529px', data: data});
      dialogRef.afterClosed().subscribe(result => {});    
   }

And the dialog template:

<md-tab-group  class="follow-dialog">
<md-tab  label="tab 1" id="followers-tab" md-active="data.type == 1">                            
    tab 1 content
</md-tab>
<md-tab  label="tab 2" id="following-tab" md-active="data.type == 2">                            
    tab 2 content
</md-tab>

The problem is that tab 1 opens all the time.

+4
source share
2 answers

You need to use the attribute [selectedIndex] <md-tab-group>. Assuming what a data.typerepresents tabIndex, you can do the following:

<md-tab-group class="follow-dialog" [selectedIndex]="data?.type-1">
    <md-tab label="tab 1" id="followers-tab">
        tab 1 content
    </md-tab>
    <md-tab label="tab 2" id="following-tab">
        tab 2 content
    </md-tab>
</md-tab-group>

md-active, . , 0, 1 : [selectedIndex]="data?.type-1"

-

+4

:

openDialog(tab: number) {
    let dialogRef = this.dialog.open(TwoTabDialog);
    this.dialogRef.componentInstance.activeTab = tab - 1;
    dialogRef.afterClosed().subscribe(result => {});    
}

<md-tab-group  [selectedIndex]="activeTab">
    <md-tab  label="tab 1" id="followers-tab">                            
        tab 1 content
    </md-tab>
    <md-tab  label="tab 2" id="following-tab">                            
        tab 2 content
    </md-tab>
</md-tab-group>
0

Source: https://habr.com/ru/post/1684605/


All Articles