我有一个循环结构,其中发布的数据(例如内容文本,照片,视频)在单击“赞”按钮时应该为数据做什么,它应该只选择特定的“喜欢”索引数据。

由于我是离子和角接触的新手,因此我尝试在按钮单击事件中以索引i调用该数据,但它并不代表我返回任何东西。

.html file
  <ion-button fill="clear" (click)="likebutton(i)">Like {{numberoflike[i]}}</ion-button>


.ts file
  likebutton(){
    this.numberoflike++;
  }


我想选择任何一个与数据代表相似的人,并仅显示给特定的相似按钮。
如果可能的话,请让我承认如何使用* ngFor以及如何传递内部参数和值fof

最佳答案

你可以这样

.ts文件

export class MyComponent implements OnInit {
numberoflike:number[]=[];
itemsArray:number[]=[1,2,3,4,5,6,7];

    likebutton(i){
    this.numberoflike[i]++;//increment the count with given index
  }
  ngOnInit() {
    this.itemsArray.forEach(()=>this.numberoflike.push(0));//initialize the likes array
  }

}


.html文件

  <ion-card type="md" *ngFor="let item of itemsArray;let i=index;">
        <ion-item (click)="likebutton(i)">
            Like {{numberoflike[i]}}
        </ion-item>
                 </ion-card>

09-18 14:33