md-tabs Using code to goto a tab in angular 2 material design

后端 未结 2 1288
暖寄归人
暖寄归人 2020-12-06 14:23

I am using Angular2 to create an application i which i need to navigate between tabs using a button present inside the tabs.

my html is as the following :

         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 14:47

    This can be achieved using selectedIndex input variable using the below code

    
      Content 1 
      
      
      Content 2
    
    

    and your typescript code will be

    @Component({
      selector: 'tabs-sample',
      templateUrl: './tabsSample.html',
    })
    export class TabsSample {
      selectedIndex:number=0;
      clickMe(){
        this.selectedIndex=1;
      }
    
    }
    

    Update 1: Based on comment.

    You can use the selectedIndexChange method to fix that error as below

    
    
    
    

    and your code will have

    selectedIndexChange(val :number ){
        this.selectedIndex=val;
      }
    

    Updated in the plunk as well.

    LIVE DEMO

提交回复
热议问题