【问题标题】:change text dynamically based on checkbox value根据复选框值动态更改文本
【发布时间】:2019-09-27 15:55:44
【问题描述】:

使用FormArray我的组件上有绑定复选框,复选框的默认label选择,当我点击复选框时,label将变为选中对于每个复选框(应该是),我还得到了 selectAll 按钮,它将所有复选框的值更改为 true 并将文本更改为 selected,我的问题是:

  • 当我只选择一个复选框时,更改label 也会影响其他复选框
  • 当我有selectAll复选框时,所有复选框标签将更改为选中,然后如果我取消选中单个复选框,它应该将label更改为select

这是我尝试过的:

html 文件

<div *ngIf="isShowResponse">
    <p>Inquiry Response</p>
    <form [formGroup]="form" (ngSubmit)="submitSelectedCheckboxes()">
        <ng-container formArrayName="receivedSummons" *ngFor="let summon of formReceivedSummons.controls; let i = index">
            <ng-container [formGroupName]="i">
                <input type="checkbox" formControlName="isChecked"
        (change)="checkedState(summon)">
            {{ summonText }}
           </ng-container>
       </ng-container>
   </form>
<!-- <pre>{{form.value | json}}</pre> -->
<button (click)="selectAll()">Select All</button>
</div>

ts 文件

    checkedState(summon) {
    summon.checked = !summon.checked;
    this.summonText = summon.checked === true ? 'selected' : 'select';
  }

  selectAll() {
    this.formReceivedSummons.controls.map(value => value.get('isChecked').setValue(true));
    return this.summonText = 'selected';
  }

这是我完整的stackblitz demo,我可以使用一些建议和更好的做法来解决这个问题

【问题讨论】:

    标签: javascript angular typescript checkbox


    【解决方案1】:
    <div *ngIf="isShowResponse">
        <p>Inquiry Response</p>
        <form [formGroup]="form" (ngSubmit)="submitSelectedCheckboxes()">
            <ng-container formArrayName="receivedSummons" *ngFor="let summon of formReceivedSummons.controls; let i = index">
                <ng-container [formGroupName]="i">
                    <input type="checkbox" formControlName="isChecked">
      {{ summon.get('isChecked').value ? 'selected' : 'select' }}
          </ng-container>
        </ng-container>
    </form>
    <!-- <pre>{{form.value | json}}</pre> -->
    <button (click)="selectAll()">Select All</button>
    </div>
    
    

    https://stackblitz.com/edit/angular-43suh3?file=src/app/inquiry-response/inquiry-response.component.html

    【讨论】:

      【解决方案2】:

      我做了一些改动,你可以试试这个:

      HTML

      <div *ngIf="isShowResponse">
      <p>Inquiry Response</p>
      <form [formGroup]="form" (ngSubmit)="submitSelectedCheckboxes()">
          <ng-container formArrayName="receivedSummons" *ngFor="let summon of formReceivedSummons.controls; let i = index">
              <ng-container [formGroupName]="i">
                  <input type="checkbox" [checked]="summon.checked" formControlName="isChecked"
          (change)="checkedState(summon)">
             {{ summon.checked === true ? 'selected' : 'select' }}
          </ng-container>
       </ng-container>
      </form>
       <!-- <pre>{{form.value | json}}</pre> -->
      <button (click)="selectAll()">Select All</button>
      </div>
      

      在函数中:

      selectAll() {
       this.formReceivedSummons.controls.map((summon: any) => {
        summon.checked = true;
       });
      }
      checkedState(summon) {
       summon.checked = !summon.checked;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 2018-02-19
        • 2013-02-02
        相关资源
        最近更新 更多