亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從復選框列表中返回所有選中和未選中的值?

如何從復選框列表中返回所有選中和未選中的值?

烙印99 2022-05-26 10:39:11
stackblitz 示例 我正在使用以下數據對象創建復選框列表:data = [ { Key: "class_id", displayName: "Section ID", enabled: true }, { Key: "room_l4", displayName: "Location", enabled: false }, { Key: "section", displayName: "Section", enabled: true }, { Key: "section_2", displayName: "Section 2", enabled: true }, { Key: "campus", displayName: "Description", enabled: true } ] 當用戶選擇復選框時,我想將“啟用”從 false 更改為 true,反之亦然。單擊提交按鈕后,我想以現在的方式控制臺記錄數據對象,除了“啟用”字段的更改值。例如,如果用戶取消選中 data[0].enabled 的復選框,那么我的預期結果將是: data = [ { Key: "class_id", displayName: "Section ID", enabled:false }, { Key: "room_l4", displayName: "Location", enabled: false }, { Key: "section", displayName: "Section", enabled: true }, { Key: "section_2", displayName: "Section 2", enabled: true }, { Key: "campus", displayName: "Description", enabled: true } ] app.component.tsimport { Component, OnInit } from '@angular/core';import { FormGroup, FormControl } from '@angular/forms';@Component({selector: 'my-app',templateUrl: './app.component.html',styleUrls: [ './app.component.css' ]})export class AppComponent implements OnInit {myForm: FormGroupname = 'Angular';data = [  { Key: "class_id", displayName: "Section ID", enabled: true },  { Key: "room_l4", displayName: "Location", enabled: false },  { Key: "section", displayName: "Section", enabled: true },  { Key: "section_2", displayName: "Section 2", enabled: true },  { Key: "campus", displayName: "Description", enabled: true }  ]  onSubmit(e){  console.log(e)  }  ngOnInit() {  this.myForm = new FormGroup({  name: new FormControl('valu'),  });  }  }app.component.html    <form  [formGroup]="myForm" (ngSubmit)="onSubmit(myForm)">     <ul class="list-group list-group-flush" *ngFor="let a of data">        <li class="list-group-item">          <input            formControlName="name"             (click)="onSubmit()"            type="checkbox"            [checked]="a.enabled"          />          {{ a.displayName }}        </li>      </ul>      <button>Submit</button>      </form>
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

FormArray您可以使用 a為您的數據構建表單。使用 收聽變化valueChanges。


組件.ts


form: FormGroup


private formControls: {

  data: FormArray;

};


ngOnInit() {

  this.formControls = {

    data: new FormArray(this.data.map(x => {

      return new FormGroup({

        enabled: new FormControl(x.enabled)

      });

    }))

  };


  this.formControls.data.valueChanges.subscribe(() => {

    this.data.forEach((item, i) => {

      item.enabled = this.formControls.data.controls[i].get('enabled').value;

    });

  });


  this.form = new FormGroup(this.formControls);

}

組件.html


<form  [formGroup]="form" (ngSubmit)="onSubmit(myForm)">

  <ul class="list-group list-group-flush" formArrayName="data"

    *ngFor="let a of data; index as i">

    <li class="list-group-item" [formGroupName]="i">

      <label>

        <input formControlName="enabled" type="checkbox" />

        {{ a.displayName }}

      </label>

    </li>

  </ul>

  <button>Submit</button>

</form>

初始表單數組是根據您的數據數組構建的。


表單指令反映了表單的結構。請注意如何[formGroupName]="i"綁定到i數組中的第 th 表單組。


演示:https ://stackblitz.com/edit/angular-88kjfq


查看完整回答
反對 回復 2022-05-26
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

有一個選項可以簡單地使用[(ngModel)]和避免使用表單。一起工作this.data

https://stackblitz.com/edit/angular-ach8xw?file=src%2Fapp%2Fapp.component.html


查看完整回答
反對 回復 2022-05-26
  • 2 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號