简体   繁体   English

如何知道哪个 object 在阵列中是最后一个? Angular(angular-material 标签)

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

I have array of object我有一系列 object

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

array can have many objects.数组可以有很多对象。 I have function which add object:我有 function 添加 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.还有 function 删除并返回路由器。

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

html: 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.问题是当删除不活动的选项卡时,go 到回路线..我不知道要解决这个问题。 Maybe how to check the active route in url???也许如何检查 url 中的活动路线?

You can make use of ' selectFocusedIndex ' event or 'selectedIndex' property to keep track of active tab.您可以使用“ selectFocusedIndex ”事件或“selectedIndex”属性来跟踪活动选项卡。 Also add condition to check active tab in your remove tab function for navigating back to the previous route.还要添加条件以检查删除选项卡中的活动选项卡 function 以导航回上一个路线。

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

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