2

I have created two radio button for gender, I want to display radio button checked that was previously selected.

I have added below code in Template

<div class="form-group">
    <label>Gender</label>

    <div id="input-type">
        <label class="radio-inline">
            <input type="radio" formControlName="gender" [name]="'gender'" [value]="'male'"/> Male
        </label>
        <label class="radio-inline">
              <input type="radio" formControlName="gender" [name]="'gender'" [value]="'female'"/> Female
        </label>
    </div>
</div>

I have added below code in Component.

console.log(this.gender);  // I am getting 'male' here.
this.editProfile = new FormGroup({
     gender: new FormControl(this.gender || null)
});

Other value of the form is getting displayed but radio button is not getting checked.

2
  • what attribute is [name] and [value] ? Commented Mar 3, 2017 at 6:26
  • Name and value of Radio button, But i have also tried without name. Commented Mar 3, 2017 at 6:28

3 Answers 3

1

You can write it like

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

    <label class="radio-inline">
            <input type="radio" formControlName="gender" [name]="'gender'" [value]="'male'"/> Male
        </label>
        <label class="radio-inline">
              <input type="radio" formControlName="gender" [name]="'gender'" [value]="'female'"/> Female
        </label>
     <button type="submit" [disabled]="form.invalid">Submit</button>

    {{form.value|json}}
  </form>

It should work for you

Find a PLUNK here

Sign up to request clarification or add additional context in comments.

Comments

1

I have found other solution. We can use below code

<div class="form-group">
    <label>Gender</label>
      <div id="input-type">
          <label class="radio-inline">
              <input type="radio" [checked]="editProfile.value.gender == 'male'" formControlName="gender" value="male"/> Male
          </label>
          <label class="radio-inline">
                <input type="radio" [checked]="editProfile.value.gender == 'female'" formControlName="gender" value="female"/> Female
          </label>
      </div>
</div>

Comments

0

As you are using Reactive Forms you can omit name attribute on input and because you are setting string value to input so you have to use value="male" instead of [value]="'male'".

<div class="form-group">
  <label>Gender</label>

  <div id="input-type">
    <label class="radio-inline">
        <input type="radio" formControlName="gender" value="male"/> Male
    </label>
    <label class="radio-inline">
          <input type="radio" formControlName="gender" value="female"/> Female
    </label>
  </div>
</div>

2 Comments

I have tried this but it is not working, still I am not getting checked
check by setting static value for gender field inside form control like gender: new FormControl('male') to see if its checked or not. May be issue with this.gender value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.