简体   繁体   English

如何以反应形式存储布尔值?

[英]How to store booleans in a reactive form?

I want to store booleans in a reactive form where a button has the value true or false. 我想以反应形式存储布尔值,其中按钮的值为true或false。 So that I can access the inputs of these buttons. 这样我就可以访问这些按钮的输入。 The problem is as well that I have another form in this page, for text inputs. 问题在于我在此页面中还有另一种形式用于文本输入。 So I don't really know if I can use the form twice. 所以我真的不知道我是否可以两次使用该表格。

<ion-form  [formGroup]="form" (ngSubmit)="takeBools()">
    <ion-grid>
    <ion-row>
    <ion-col>
        <ion-button  class="button1" (click)="onLoc()" shape="round" [color]="local ? 'light' : 'grey'"> <ion-icon name="pin"></ion-icon> </ion-button>
    <ion-col>
    <ion-row>
    <ion-grid>
</ion-form>

<form [formGroup]="form" (ngSubmit)="addTag()">
    <ion-item>
      <ion-input formControlName="tag" clearInput="true" placeholder="Tags" name="tagValue"></ion-input>
      <ion-button item-right type="submit" icon-only>
      <ion-icon name="checkmark"></ion-icon>
    </ion-button>
    </ion-item>
  </form>

page.ts page.ts

  form: FormGroup;
    public local: boolean = true;
    ...



addTag() { // properly access and reset reactive form values
      const tagCtrl = this.form.get('tag');
      if (tagCtrl.value) {
        this.tagList.push(tagCtrl.value);
        this.tagInput = ''; // in order to have an empty input
      }
    }

        takeBools() {
          const local: boolean = this.form.controls['local'].value;
          console.log(this.local);
        }

      ngOnInit() {
     this.form = new FormGroup({
      tag: new FormControl(null, {
        updateOn: 'submit',
        validators: [Validators.required, Validators.maxLength(20), Validators.minLength(1)]
      }),
      local: new FormControl(true, {    // these bools are not accesable yet
        updateOn: 'submit',
      }),
        })

I think what you're trying to do is to pull the value of local out of the FormGroup . 我认为您要尝试的是将local的值从FormGroup

You can do that with the following code. 您可以使用以下代码来实现。

const local: boolean = this.form.controls[‘local’].value;

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

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