简体   繁体   English

Angular 2 如何“观察”选项卡的变化

[英]Angular 2 How to “watch” for tab changes

I have:我有:

<md-tab-group color="primary">
  <md-tab label="Проэкты">
    <h1>Some tab content</h1>
  </md-tab>
  <md-tab label="Обучалка">
    <h1>Some more tab content</h1>
    <p>...</p>
  </md-tab>
</md-tab-group>

I need to catch an event when a specific tab is clicked and call this function inside my component:我需要在单击特定选项卡时捕获一个事件并在我的组件内调用此函数:

onLinkClick() {
  this.router.navigate(['contacts']); 
}

You could use the (selectedTabChange) event .您可以使用(selectedTabChange)事件 Check Material2#tabs .检查Material2#tabs

Template:模板:

<mat-tab-group color="primary" (selectedTabChange)="onLinkClick($event)">
  ...
</mat-tab-group>

Component :组件

import { MatTabChangeEvent } from '@angular/material';

// ...

onLinkClick(event: MatTabChangeEvent) {
  console.log({ event });

  this.router.navigate(['contacts']); 
}

You need to publish the event as an @Output from you md-tab component.您需要从md-tab组件将事件发布为@Output Something like:就像是:

import { EventEmitter, Output, Input, Component } from '@angular/core';

@Component({
    selector: 'tab',
    template: `
        <button (click)="clicked()">{{ name }}</button>
    `,
    styles: [`
    `]
})
export class TabComponent {
    @Input() name = 'replaceme';
    @Output() tabClicked = new EventEmitter<null>();

    clicked() {
        this.tabClicked.emit();
    }
}

Then you consume that event in the md-tab-group , something like this:然后您在md-tab-group使用该事件,如下所示:

import { Component }          from '@angular/core';

@Component({
    selector: 'tab-group',
    template: `
        <!--<tab *ngFor="let tab of tabs" [name]="tab"></tab>-->
        <tab *ngFor="let tab of tabs" [name]="tab" (tabClicked)="tabChanged(tab)"></tab>
        <div>
        {{ selectedTab }}
        </div>
    `,
    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这是一个演示这个概念的工作plunker

it can be done easily as follows它可以很容易地完成如下

 <md-tab-group color="primary" (click)="selectedTabIndex=tabRef.selectedIndex" #tabRef>
      <md-tab label="Проэкты">
        <h1>Some tab content</h1>
      </md-tab>
      <md-tab label="Обучалка">
        <h1>Some more tab content</h1>
        <p>...</p>
      </md-tab>
    </md-tab-group>

only thing to do is defining a global variable in your component.唯一要做的就是在你的组件中定义一个全局变量。

 export class MyComponent implements OnInit{   
 selectedTabIndex=0;
     ngOnInit(){
         // 
       }
    }

Documentation says, that the content of the you tab is not injected into the DOM until the tab is activated.文档说,在激活选项卡之前,您选项卡的内容不会注入到 DOM 中。 So, you may catch DOM events on you components with @HostListener annotation:因此,您可以使用@HostListener注释捕获组件上的 DOM 事件:

<md-tab-group color="primary">
  <md-tab label="Проэкты">
    <my-cool-tab1></my-cool-tab1>
  </md-tab>
  ...
</md-tab-group>

Component:成分:

@Component({selector: 'my-cool-tab1', ...})
export class MyCoolTab1Component {

  @HostListener('DOMNodeInsertedIntoDocument')
  foo() {
    console.log('Tab activated');
  }

  @HostListener('DOMNodeRemovedFromDocument')
  foo2() {
    console.log('Tab deactivated');
  }
}

update: this not works in FF %(更新:这在 FF %(

You can use ngKeypress ( Angular Documentation here ) into any HTML tag.您可以任何 HTML 标签中使用ngKeypress此处为 Angular 文档)。 For example:例如:

<anyHtmlTag ng-keypress="yourFunction($event)">  

yourFunction(evt){
   if(evt.code === "Tab"){
      //Do your stuff
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM