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";
}
ng-model="selectedOption"should be[(ngModel)]="selectedOption"[value]="1"to make it anumber, instead of a string, and then trythis.selectedOption=1. Your syntax looks good but I remember facing a similar issue herengModelanyway. Just assigning the value to the controls should do, likethis.signUpDetailsForm.get('details').setValueAndValidity('3')