10

I have a radio group that I want to set the value of from my component code. However I cannot seem to set the initial value using [(ngModel)]. I don't get any errors or anything to show why it is not showing the radio button selected.

<div class="form-group">
  <mat-radio-group  [(ngModel)]="selectedStatus" formControlName="completed">
    <mat-radio-button  [value]="1">Call Complete</mat-radio-button>
    <mat-radio-button [value]="2">Call Incomplete</mat-radio-button>
  </mat-radio-group>
</div>

Component code snippets:

selectedStatus: Array<string>;

private initForm() {
    this.eventEditForm = new FormGroup({          
      'completed': new FormControl()
      });          
      this.selectedStatus = this.data[0].completed;
    }

this.data[0].completed returns 1 or 2 from a data service.

3
  • Well, selectedStatus is supposed to be 1, or 2. But you declared it as an Array<string>. It should be a number, because 1, and 2, are numbers. The compiler error message should tell you that. Why don't you read it? Commented Dec 2, 2017 at 16:05
  • The compiler is not showing any errors. I had tried it with number and it had no effect. So a little confused. Commented Dec 2, 2017 at 16:08
  • Post a complete minimal example reproducing the problem, in a plunkr. Commented Dec 2, 2017 at 16:09

1 Answer 1

2

Your variable selectedStatus should not be a array of strings, it should be a number, change it as.

selectedStatus:  number ;  

WORKING STACKBLITZ

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

2 Comments

stackblitz link is dead :-(
In Angular Material 7, formControlName becomes -> name (@Input() name: string). material.angular.io/components/radio/api "Name of the radio button group. All radio buttons inside this group will use this name."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.