Home > Net >  Angular Material tab change (previous and current tab)
Angular Material tab change (previous and current tab)

Time:07-15

Is there a way we can check tab change or detect if previous tab is equal to current tab? Let us say my current tab is 1 and then I change tab which current value now is 2, how do we check if previous is equal to the current tab without saving it to a local storage?

 tabChange(event: MatTabChangeEvent) {
    this.selectedIndex = event.index;
    this.selectedBrokerProperty = [];
    this.files = [];
  }

CodePudding user response:

I'd just add a lastTabIndex: number property to the same class that has tabChange. Then in tabChange you could compare this.lastTabIndex (previous) to event.index (current).

At the end of tabChange, add this.lastTabIndex = event.index; and you're good to go.

Maybe in ngOnInit you could set this.lastTabIndex = <reference to MatTabGroup>.selectedIndex; to initialize to the correct state. That's optional, as the value would be undefined, which is also not equal to event.index... Although if you needed to know previous for the first tabChange call, it would be necessary.

  • Related