是否可以在*ngFor内增加组件属性值



我想为每次迭代在ngFor中增加一个类属性1。请告诉我是否有办法。提前谢谢。

组件类别:

class AA
{      
property:number = 0;
}

模板:

<div *ngFor = "let sample of samples">
//increment property here
</div>

您可以使用index

<div *ngFor = "let sample of samples;let i = index">
<span [class]="'opacity-'+(i+1)">value {{i+1}}</span>
<button (click)="property = (i+1)">Set Selected Item</button>
</div>
Selected Item : {{property}}

只需在类中创建一个函数,并将其调用到ngforblock

class AA
{      
property:number = 0;

function increment_property(): void{
this.property++;
}
}

<div *ngFor = "let sample of samples">
{{ increment_property() }}
</div>

最新更新