首页 > 解决方案 > 我想在 for 循环中显示特定记录的标签文本

问题描述

<div ngFor="let data of Requests">
          FirstName:<b>{{data.name}}</b>&nbsp;
          <button (click)="AcceptRequest()">Accept</button>
          <button (click)="CancelRequest()">Decline</button>
          <label>{{RequesStatus}}</label>

我只想在我为每个请求显示的代码中显示接受或拒绝该请求的请求的标签。

标签: angular7

解决方案


试试这个,这应该工作:

<div *ngFor="let item of requests; index as ix">
    ix: {{ix}} FirstName:<b>{{item.name}}</b>&nbsp;
    <button (click)="HandleRequest(true, ix)">Accept</button>
    <button (click)="HandleRequest(false, ix)">Decline</button>
    <label>{{requesStatus[ix]}}</label>

    <!-- or -->
    <label>{{(isAccepted[ix]) ? 'accepted' : 'declined'}}</label>
</div>

在 TS

                     // in class level declare this
   requesStatus: string[] = [];
                     // or
   isAccepted: boolean[] = [];

                     // function to set and use in template
   HandleRequest(accepted, ix) {
      this.requesStatus[ix] = (accepted) ? 'accepted' : 'declined';
                     // or
      this.isAccepted[ix] = accepted;
   }

推荐阅读