Angular 2: ngFor 索引仅在满足大小写时递增
Angular 2: ngFor index only increment if case met
假设我有一组要循环的对象:
arr = [ {a: 1}, {a: 2}, {a: 1} ]
并且我想在我的视图中显示对象的索引,但我只希望索引在满足条件时递增。类似于 a == 1
。因此,如果我有一些代码,例如:
<!-- I know this won't work -->
<ul>
<li *ngFor="let obj of arr; let i = index">{{ i }}</li>
</ul>
...它将显示为:
<li>0</li>
<li></li>
<li>1</li>
app.component.ts:
arr:{a:number}[] = [{a: 1},{a: 2},{a: 1} ];
app.component.html
<ul>
<div *ngFor="let obj of arr; let i = index">
<div *ngIf="obj.a == 1">
<li>{{i}}</li>
</div>
</div>
</ul>
假设我有一组要循环的对象:
arr = [ {a: 1}, {a: 2}, {a: 1} ]
并且我想在我的视图中显示对象的索引,但我只希望索引在满足条件时递增。类似于 a == 1
。因此,如果我有一些代码,例如:
<!-- I know this won't work -->
<ul>
<li *ngFor="let obj of arr; let i = index">{{ i }}</li>
</ul>
...它将显示为:
<li>0</li>
<li></li>
<li>1</li>
app.component.ts:
arr:{a:number}[] = [{a: 1},{a: 2},{a: 1} ];
app.component.html
<ul>
<div *ngFor="let obj of arr; let i = index">
<div *ngIf="obj.a == 1">
<li>{{i}}</li>
</div>
</div>
</ul>