简体   繁体   English

Angular Material 8 单选按钮值作为 boolean

[英]Angular Material 8 radio button value as a boolean

I have a simple website with 2 properties: One radio button group (with 2 radio buttons) and an input field (text field)我有一个具有 2 个属性的简单网站:一个单选按钮组(带有 2 个单选按钮)和一个输入字段(文本字段)

What I'm trying to do is once the user submits the form, the boolean value of the selected radio button and the value of the input field should be saved.我要做的是一旦用户提交表单,应保存所选单选按钮的 boolean 值和输入字段的值。 For the input field this works without any problem, but for some reason the value of the radio button is just left out and doesn't appear anywhere.对于输入字段,这没有任何问题,但由于某种原因,单选按钮的值被遗漏了并且没有出现在任何地方。 This is what I got so far:这是我到目前为止得到的:

<mat-radio-group label="Select an option" formControlName="membership" #membership>
   <mat-radio-button [value]=true color="primary">Yes</mat-radio-button>
   <br>
   <mat-radio-button [value]=false color="primary">No</mat-radio-button>
</mat-radio-group>

The submission button:提交按钮:

<button mat-button type="submit" (click)="addWish(membership.value, wish.value)">Submit</button>

And the form builder as follows:表单生成器如下:

this.createForm = this.fb.group({
      membership: [[false], Validators.required],
      wish: ''
    });

Does anyone have an idea why the membership value is just left out at submission?有谁知道为什么membership价值在提交时被遗漏了? Thanks in advance!提前致谢!

How about adding ngModel:添加ngModel怎么样:

<mat-radio-group label="Select an option" name="membership" [(ngModel)]="membership">
                <mat-radio-button [value]=true color="primary">Yes</mat-radio-button>
                <br>
                <mat-radio-button [value]=false color="primary">No</mat-radio-button>
             </mat-radio-group>
             <button mat-button type="submit" (click)="addWish(membership)">Submit</button>

and in.ts:和 in.ts:

addWish(item){
  console.log(item);
}

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

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