Submit form mat-radio-group

Pravu :

When I try submit form and when I console.log() it only appear null without value. This my demo code and stackblitz

HTML

<form [formGroup]="changeNotifyForm" (ngSubmit)="onSubmit()">
<mat-radio-group class="example-radio-group" name="favoriteSeason" [(ngModel)]="favoriteSeason" [ngModelOptions]="{standalone: true}">
  <mat-radio-button class="example-radio-button" *ngFor="let season of seasons" [value]="season">
    {{season}}
  </mat-radio-button>
</mat-radio-group>
<button>submit</button>
</form>

Component

changeNotifyForm:FormGroup;
   constructor(private fb: FormBuilder){
      this.changeNotifyForm = fb.group({
        notify: ['', Validators.required]
      });
    }
onSubmit() {
  const notifys = this.changeNotifyForm.value;
  console.log(notifys)
}
Prince :

You do not need to use ngModel as you are already using reactive form. You just need to bind the formControl instance in component class with template using formControlName.

<mat-radio-group class="example-radio-group" name="favoriteSeason" formControlName="notify">

Please find working code here: https://stackblitz.com/edit/angular-fhh6pp-nzroy9

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=410705&siteId=1