1

Here is the code in html file :

       <div class="form-group">
            <div class="form-text">Some Question about Email and Phone Details?</div>


            <div>
                <input type="radio" value="1" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="EmailClicked()"
                ng-model="selectedOption"> Email</div>

            <div>

Text Both

in the component.ts class, I am setting the selectedOption to 1. Like this :

export class TestComponent implements OnInit{
  signUpDetailsForm: FormGroup;
  public submitted : boolean;

  public selectedOption : string ='1';

I can see the selectedOption value is set properly as in ngOnInit the value is printed as 1 :

ngOnInit() {

 console.log('selectedOption='+this.selectedOption);

}

I also tried another way of solving this by setting selectedOption in ng-init but still doesn't work .

<div ng-init="selectedOption=1">
                <div>
                    <input type="radio" value="1" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="EmailClicked()"
                    ng-model="selectedOption"> Email</div>

                <div>
                    <input type="radio" value="2" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="PhoneClicked()"> Text</div>
                <div>
                    <input type="radio" value="3" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="BothClicked()"> Both</div>

</div>

UPDATE

my updated code looks like this and it still doesn't select radio by default:

<div class="form-group">
            <div class="form-text">Some Question about Email and Phone Details?</div>


            <div>
                <input type="radio" value="1" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="EmailClicked()"
                [(ngModel)]="selectedOption"> Email</div>

            <div>
                <input type="radio" value="2" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="PhoneClicked()"> Text</div>
            <div>
                <input type="radio" value="3" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="BothClicked()"> Both</div>


        </div>

//This is in component.ts class:

public selectedOption :string;
ngOnInit() {

this.selectedOption="1";

}
8
  • 4
    Are you mixing angular 1 with 2? Commented Mar 2, 2017 at 16:30
  • ng-model="selectedOption" should be [(ngModel)]="selectedOption" Commented Mar 2, 2017 at 16:33
  • I also don't think ng-init is a thing in angular 2, so that line should be removed. Commented Mar 2, 2017 at 16:33
  • 3
    use [value]="1" to make it a number, instead of a string, and then try this.selectedOption=1. Your syntax looks good but I remember facing a similar issue here Commented Mar 2, 2017 at 16:57
  • 1
    If you use reactive forms you should IMHO not use ngModel anyway. Just assigning the value to the controls should do, like this.signUpDetailsForm.get('details').setValueAndValidity('3') Commented Mar 2, 2017 at 17:03

2 Answers 2

7

got hint from William's comment. answer posted here:

@Component({
  selector: 'my-app',
  template: `

<form>

     <div class="container">
        <div class="row">
            <div class="col-md-2"></div>
            <header class="header col-md-8">
<div class="form-group">
                <div class="form-text">Some Question about Email and Phone Details?</div>


                <div>
                    <input type="radio" [value]="1"  name="details" [(ngModel)]="selectedOption">
                    Email</div>

                <div>
                    <input type="radio" [value]="2"  name="details" [(ngModel)]="selectedOption"> Text</div>
                <div>
                    <input type="radio" [value]="3"  name="details" [(ngModel)]="selectedOption"> Both</div>

            </div>

            </header>

        </div>
     </div>
</form>
  `,
})
export class App {
   public selectedOption :number =3;
  constructor() {

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

Comments

0

I had a similar problem but using angular mat-radio-group and binding of the data to the form-group. Instead of two-way binding to your model, bind the result of the selection to the model and another value to the form-group.

On the other hand, I think you don't need to bind the values in the radio-buttons, just specify the value that the radio button will emit to your model if it is selected.

@Component({
  selector: 'my-app',
  template: `

<form>

 <div class="container">
    <div class="row">
        <div class="col-md-2"></div>
        <header class="header col-md-8">
<div class="form-group" [value]= "this.selectedOption">
            <div class="form-text">Some Question about Email and Phone 
Details?</div>


            <div>
                <input type="radio" value=1  name="details" 
(ngModel)="selectedOption">
                Email</div>

            <div>
                <input type="radio" value=2  name="details" 
(ngModel)="selectedOption"> Text</div>
            <div>
                <input type="radio" value=3  name="details" 
(ngModel)="selectedOption"> Both</div>

        </div>

        </header>

    </div>
 </div>
</form>
  `,
})
export class App {
  public selectedOption :number =3;
  constructor() {

  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.