【问题标题】:When clicking on like button it should show to particular data?单击“赞”按钮时,它应该显示特定数据吗?
【发布时间】:2019-05-24 05:25:27
【问题描述】:

我有一个循环结构,其中包含帖子中的数据,例如内容文本、照片、视频,当单击“赞”按钮时,我应该对数据做什么,它应该只选择特定的索引数据。

由于我是 ionic 和 angular 的新手,我尝试在按钮单击事件中使用索引 i 调用该数据,但它并不代表我返回任何东西。

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

我想用代表我的数据选择任何一个喜欢的人,并只显示给特定的喜欢按钮。 如果可能的话,请让我了解如何使用 *ngFor 并传递它的内部参数和值

【问题讨论】:

    标签: javascript json angular ionic-framework


    【解决方案1】:

    你可以这样做,

    .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>
    

    【讨论】:

    • 感谢 arunkumar 的快速响应,但它在创建每个结构的循环时遇到了问题。并在其他循环数据中采用了该值。
    • 有什么问题?
    • 在调用itemsArray[1]时,它适用于循环的每个数据,不会将值推送到单个帖子。
    • 为什么要调用 itemsArray[1]?应该是 itemsArray[i] 吧?
    • 在您在构造函数 itemsArray[] 上方调用的 ts 端,我尝试调用它然后它应用但没有得到实际..
    猜你喜欢
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多