我正在使用 *ngFor 循环,但我只希望循环在数组中定义的特定索引上运行(即 indexArray = [454,640])。
如果我尝试这样做,并且 indexArray 有两个或更多元素,则它不起作用。但如果数组只有一个元素(即indexArray=[565]),它就可以工作。
<div *ngFor="let item of items; index as i">
<table *ngIf="i == indexArray">
最佳答案
您可以使用.indexOf(i)
并检查它是否在您的indexArray
变量中。
<div *ngFor="let item of items; index as i">
<table *ngIf="indexArray.indexOf(i)> -1">
<!-- REST OF THE CODE -->
关于javascript - Angular 在数组中定义的特定索引上运行 *ngFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65118785/