简体   繁体   English

如何在反应式 angular 表单中使用复选框

[英]how to use checkbox in reactive angular form

I am new to angular, currently I am looking checkboxes in angular, I have three checkboxes and I have to show checked or unchecked checkboxes in UI.我是 angular 的新手,目前我正在查看 angular 中的复选框,我有三个复选框,我必须在 UI 中显示选中或未选中的复选框。

I am getting enabled/disabled from json, I need to show if am getting enabled means checkbox should be checked and disabled means unchecked checkbox.我正在从 json 启用/禁用,我需要显示是否启用意味着应选中复选框,禁用意味着未选中复选框。

This is what am trying in html这就是我在 html 中尝试的

 <form [formGroup]="portFilterGroup">
      <div class="form-group row">
            <div class="col-md-4 text-left" id="email">
                <label for="email"><input type="checkbox" (change)="onChecked($event)"  formcontrolName="emailData"  value="{{emailData}}" [checked]="isChecked"  >&nbsp;
                    <b>Email(POP3, IMAP, SMTP)</b></label>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-md-4 text-left" id="ftp">
                <label for="ftp"><input type="checkbox" formcontrolName="ftpData"  [checked]="isChecked" value="{{ftpData}}">&nbsp; <b>FTP</b></label>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-md-4 text-left" id="http">
                <label for="http"><input type="checkbox" formcontrolName="httpData"
                    [checked]="isChecked">&nbsp;<b>HTTP</b></label>
            </div>
        </div>
    </form>

typescript file typescript 文件

  portFilterForm(){
    this.portFilterGroup = this.fb.group({
      emailData: new FormControl(''),
      ftpData: new FormControl(''),
      httpData: new FormControl('')
    })
  }

 
  onChecked(e){  
    console.log("e", e.target) 
    if (e.target == 'enabled'){
      this.isChecked = true; 
    }
    else{
      this.isChecked = false;
    }
  }

  gettingData(){
  this.httpClient.get("assets/json/data.json").subscribe((data:any) =>{
    console.log(data);
    this.emailData = this.onChecked(data.email)
    this.httpData = this.onChecked(data.http)
 })
}

and the json file be和 json 文件是

{
    "email": "enabled",
    "ftp": "disabled",
    "http": "enabled"
}

this is the code am trying, but am not get the expecting output(enabled means checked and disabled means unchecked) can anyone help me to this?这是正在尝试的代码,但没有得到预期的输出(启用表示已检查,禁用表示未检查)有人可以帮我吗?

Ok so you can use the angular way read here好的,所以你可以使用 angular 方式阅读这里

html html

<form [formGroup]="portFilterGroup">
  <div class="form-group row">
    <div class="col-md-4 text-left" id="email">
      <label
        ><input
          type="checkbox"
          [formControl]="portFilterGroup.controls['emailData']"
        />&nbsp; <b>Email(POP3, IMAP, SMTP)</b></label
      >
    </div>
  </div>

  <div class="form-group row">
    <div class="col-md-4 text-left" id="ftp">
      <label
        ><input
          type="checkbox"
          [formControl]="portFilterGroup.controls['ftpData']"
        />&nbsp; <b>FTP</b></label
      >
    </div>
  </div>

  <div class="form-group row">
    <div class="col-md-4 text-left" id="http">
      <label
        ><input
          type="checkbox"
          [formControl]="portFilterGroup.controls['ftpData']"
        />&nbsp;<b>HTTP</b></label
      >
    </div>
  </div>
</form>
{{ portFilterGroup.value | json }}
<button (click)="gettingData()">submit</button>

ts file ts文件

import { Component } from '@angular/core';
import { FormControl, FormGroup, FormBuilder, FormArray } from '@angular/forms';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular 6';
  portFilterGroup: FormGroup;

  constructor(private fb: FormBuilder) {
    this.portFilterForm();
  }

  portFilterForm() {
    this.portFilterGroup = this.fb.group({
      emailData: new FormControl(false),
      ftpData: new FormControl(false),
      httpData: new FormControl(false),
    });
  }

  gettingData() {
    console.log(this.portFilterGroup.value);
    // this.httpClient.get('assets/json/data.json').subscribe((data: any) => {
    //   console.log(data);
    //   this.emailData = this.onChecked(data.email);
    //   this.httpData = this.onChecked(data.http);
    // });
  }
}

stackblitz堆栈闪电战

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM