Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular Material tab change (previous and current tab)

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 = [];
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading