简体   繁体   中英

How to know which object is last in array? Angular ( angular-material tabs)

I have array of object

[{id: 1, name: home , path: 'home'}, {id: 2, name: contact , path: 'contact'}]

array can have many objects. I have function which add object:

  addTab(selectAfterAdding: any) {
    if (!this.tabs.some(objectItem => objectItem.id == selectAfterAdding.id)) {
      this.tabs.push(selectAfterAdding);
    }
  }

Also function which delete and return router back.

removeTab(index: number, event: Event) {
  this.tabs.splice(index, 1); 
  this._location.back();
}

html:

<nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let tab of tabs;let i = index;"
         [routerLink]="tab.path"
         routerLinkActive #rla="routerLinkActive" 
         [active]="rla.isActive"
         class="test"
         >
        {{ tab.name }}
        <span class="span-X-icon" (click)="removeTab(i)" >  delete </span> 
      </a>
    </nav> 

        <mat-panel-title routerLink="/home" (click)="addTab({ id: 1 , name: 'home' , path: 'home'})">
              Home page
        </mat-panel-title>

Problem is when delete tab which is not active a go to back route.. I don't know to do fix this. Maybe how to check the active route in url???

You can make use of ' selectFocusedIndex ' event or 'selectedIndex' property to keep track of active tab. Also add condition to check active tab in your remove tab function for navigating back to the previous route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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