I am trying to get Angular 2 and Material 2 working together with a FormGroup
and an <md-radio>
component. However when I wire it up like I would with a standard <md-input>
if throws an error. For example
component.html
<form [formGroup]="myFormGroup (ngSubmit)="doSomething()">
<md-input #birthday formControlName="birthday" placeholder="Birthday"></md-input>
<md-radio-group formControlName="gender" align="end">
<md-radio-button value="m">Male</md-radio-button>
<md-radio-button value="f">Female</md-radio-button>
</md-radio-group>
</form>
component.ts
export class Component {
myFormGroup: FormGroup;
constructor(formBuilder: FormBuilder) {
this.myFormGroup = formBuilder.group({
birthday: [this.myModel.birthday, Validators.required],
gender: [this.myModel.gender, Validators.required]
});
}
}
The error message this gives me is:
ngModel cannot be used to register form controls with a parent formGroup directive. Try using formGroup's partner directive "formControlName" instead. Example:
<div [formGroup]="myGroup"> <input formControlName="firstName"> </div> In your class: this.myGroup = new FormGroup({ firstName: new FormControl() }); Or, if you'd like to avoid registering this form control, indicate that it's standalone in ngModelOptions: Example: <div [formGroup]="myGroup"> <input formControlName="firstName"> <input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}"> </div>
Even if I change the formgroup to:
this.myFormGroup = formBuilder.group({
birthday: [this.myModel.birthday, Validators.required],
gender: new FormControl()
});
I still receive the same error.
How do I use a material 2 <md-radio>
component together with a FormGroup in Angular 2? Any help would be appreciated.
Many thanks.
JT