79718885

Date: 2025-07-29 15:45:33
Score: 0.5
Natty:
Report link

I was able to bypass this one using selectedTabChange() method.

<mat-tab-group [(selectedIndex)]="myIndex" (selectedTabChange)="onIndexChange()">     ... </mat-tab-group>
this.myIndex = 0
this.previousIndex = 0

onIndexChange() {
    if (this.previousIndex !== this.myIndex && myCondition) {
      let text = "My message";
      if (confirm(text) == true) {
        this.previousIndex = this.myIndex;
      }
      else {
        this.myIndex= this.previousIndex;
      }
    } else {
      this.previousIndex = this.myIndex;
    }
  }

you can visually see the tab revert back to your previous one with this small snippet. Went through the documentation to understand there is no way we can block tab change. But using selectedTabChange() method, we can detect the action and perform our condition check(i am using an alertbox) and then revert the myIndex value back to original.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Gopal Sahu