Angular2 Reactive Forms formControl for radio butt

2019-02-05 12:48发布

I'm building a form in Angular2 with Reactive Forms. I'm using the FormBuilder to make a group of fields. For textboxes this works extremely well. But I can't find a formControl for radio buttons.

How does this work? Should I do <input formControlName="gender" type="radio"> just like I do with text input's?

4条回答
来,给爷笑一个
2楼-- · 2019-02-05 13:02

One small to add about Reactive forms I noticed . If the value is an integer it needs to to changed to string or else it radio button wont be selected ..

  this.jobForm = this._fb.group({
                    id: [res["job"]["id"]],
                    job_status: [res["job"]["job_status"]**["id"].toString()**,Validators.required],
                    title: [res["job"]["title"],Validators.required]
                });
查看更多
够拽才男人
3楼-- · 2019-02-05 13:06

Angular Material controls are (mostly) mature enough to use now.

Their samples mostly use ngModel, but here's how you can do it with formControlName.

<mat-radio-group formControlName="colorFilter" fxLayout="column" fxLayoutGap=".25rem">
    <mat-radio-button [value]="'Blue'">Blue things</mat-radio-button>
    <mat-radio-button [value]="'Red'">Red things</mat-radio-button>
    <mat-radio-button [value]="'Orange'">Orange things</mat-radio-button>
</mat-radio-group>

I'm using angular/flex-layout to put my buttons in a vertical column.

(Could also have done value='Blue' for a constant or [value]="blueColorName" to refer to a model property.

I believe there may be an issue with 0 because it is not a 'truthy' value so watch out if you're binding to enums (may no longer be an issue).

查看更多
太酷不给撩
4楼-- · 2019-02-05 13:15

In your component, define your radio button as part of your form:

export class MyComponent {
  form: FormGroup;
  constructor(fb: FormBuilder){
    this.form = fb.group({
      gender: ""
    });
  }
}

In your component HTML, construct your form:

<div class="form-group">
  <label>Please select your gender</label>
  <div class="row">
    <label class="md-check">
      <input type="radio" value="female" name="gender" formControlName="gender">
      Female
    </label>

    <label class="md-check">
      <input type="radio" value="male" name="gender" formControlName="gender">     
      Male
    </label>
  </div>
</div>

I assume you know how to construct the entire form with your submit button, which isn't shown here.

When the form is submitted, you can get the value of the radio button here:

let genderValue = this.form.value.gender;

This will either be "female" or "male" depending on which radio button is checked.

Hope this helps you. Good luck!

查看更多
来,给爷笑一个
5楼-- · 2019-02-05 13:19

Should I do <input formControlName="gender" type="radio"> just like I do with text input's?

Yes.

How does this work?

Form control directives use a ControlValueAccessor directive to communicate with the native element. There are different types of ControlValueAccessors for each of the possible inputs. The correct one is chosen by the selector of the value accessor directive. The selectors are based on what type the <input> is. When you have type="radio", then the value accessor for radios will be used.

查看更多
登录 后发表回答