Javascript 角度-过滤器上索引的更新

Javascript 角度-过滤器上索引的更新,javascript,arrays,angular,typescript,Javascript,Arrays,Angular,Typescript,我在模板中有一个带有*ngFor的列表: <li *ngFor="let product of products | filterProducts: selectedFilter; index as productId"> <a [routerLink]="['/product', productId]"> {{product.name}} </a> </li> {{product.name} 因此,我使用fil

我在模板中有一个带有*ngFor的列表:

<li *ngFor="let product of products | filterProducts: selectedFilter; index as productId">
    <a [routerLink]="['/product', productId]">
      {{product.name}}
    </a>
</li>

{{product.name}

因此,我使用filterProducts管道过滤具有选定选项的数组项,但当数组更改时,它不会更新模板上的productId,因此routerLink将我带到错误的详细信息页面(它在原始数组中使用productId)


有什么帮助吗?每次筛选数组中的项目时,我都需要更新模板中的productId。

似乎您正试图使用索引作为productId,我认为您应该在routerLink中使用product的某些属性,即product.id或product.productId,例如。 另外,您也不需要将索引作为productId[您也可以删除它]

<li *ngFor="let product of products | filterProducts: selectedFilter; index as productId">
    <a [routerLink]="['/product', product.productId]">
      {{product.name}}
    </a>
</li>

{{product.name}


索引为productId设置产品数组中产品项的索引。它类似于productId=products.indexOf(product)。
尝试在产品对象中使用id属性

是的,您是对的。我甚至不知道为什么我会被困在这间房子里,我只是用了另一间房子,而且它很管用。谢谢,不用担心!当你过于专注时,就会发生这种情况。它只需要一双新鲜的眼睛。享受吧!