Angular 2 How to “watch” for tab changes

前端 未结 5 1399
一生所求
一生所求 2020-12-10 00:52

I have:


  
    

Some tab content

5条回答
  •  被撕碎了的回忆
    2020-12-10 01:13

    You need to publish the event as an @Output from you md-tab component. Something like:

    import { EventEmitter, Output, Input, Component } from '@angular/core';
    
    @Component({
        selector: 'tab',
        template: `
            
        `,
        styles: [`
        `]
    })
    export class TabComponent {
        @Input() name = 'replaceme';
        @Output() tabClicked = new EventEmitter();
    
        clicked() {
            this.tabClicked.emit();
        }
    }
    

    Then you consume that event in the md-tab-group, something like this:

    import { Component }          from '@angular/core';
    
    @Component({
        selector: 'tab-group',
        template: `
            
            
            
    {{ selectedTab }}
    `, styles: [` `] }) export class TabGroupComponent { private tabs = ['foo', 'bar']; private selectedTab = this.tabs[0]; onInit() { this.selectedTab = this.tabs[0]; } tabChanged(tab) { this.selectedTab = tab; } }

    Heres a working plunker that demonstrates the concept

提交回复
热议问题