Sha
Sha

Reputation: 1974

Grouping checkboxes and displaying input field conditionally

I have the following form in my Angular application where I want to display different input field based on the checkbox selection. I have used [(ngModel)] to bind the value and with *ngIf I am trying to show/hide the text field below. This one works somewhat fine but I am not able to set 'checked' to emailField. Can anyone please tell me what am I doing wrong here.

By default I want emailField to be checked and show the corresponding input field.

<form class="reminder-form">
    <div class="form-check">
        <input  [(ngModel)]="emailField" class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
        <label class="form-check-label" for="exampleRadios1">
          Email
        </label>
      </div>
      <div class="form-check">
        <input  [(ngModel)]="smsField" class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
        <label class="form-check-label" for="exampleRadios2">
          SMS
        </label>
      </div>

      <div class="email-box" *ngIf='emailfield'>
        <label class="form-check-label" for="noRemind">
            Email
        </label>
        <input type="email" name="email" class="form-control new-reading-input" placeholder="{{accountEmail}}">
    </div>
    <div class="sms-box" *ngIf='smsField'>
        <label class="form-check-label" for="noRemind">
            SMS
        </label>
        <input type="number" name="sms" class="form-control new-reading-input" placeholder="{{accountSMS}}">
    </div>
</form>

Upvotes: 0

Views: 68

Answers (1)

shrys
shrys

Reputation: 5960

You need to bind single variable to both radio inputs:

app.component.html
<form class="reminder-form">
    <div class="form-check">
        <input  [(ngModel)]="radioValue" class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
        <label class="form-check-label" for="exampleRadios1">
          Email
        </label>
    </div>
    <div class="form-check">
        <input  [(ngModel)]="radioValue" class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
        <label class="form-check-label" for="exampleRadios2">
          SMS
        </label>
    </div>

    <div class="email-box" *ngIf='radioValue === "option1"'>
        <label class="form-check-label" for="noRemind">
            Email
        </label>
        <input type="email" name="email" class="form-control new-reading-input" placeholder="{{accountEmail}}">
    </div>
        <div class="sms-box" *ngIf='radioValue === "option2"'>
            <label class="form-check-label" for="noRemind">
            SMS
        </label>
            <input type="number" name="sms" class="form-control new-reading-input" placeholder="{{accountSMS}}">
    </div>
</form>
app.component.ts
import { Component, VERSION } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular " + VERSION.major;
  accountEmail = "[email protected]";
  accountSMS = "sms";
  radioValue = "option1";
}

Upvotes: 1

Related Questions