我正在按照http://devdocs.io/angular~2_typescript/api/forms/index/radiocontrolvalueaccessor-directive处理反应式表单单选按钮的说明并使用Angular 2.1.2和Google的MD-alpha .10 Atom-typescript说我没有错误。我收到以下控制台错误。
core.umd.js:3004 EXCEPTION: Uncaught (in promise): Error: Error in http://localhost:3000/app/components/input/postApartment4Rent.component.html:47:62 caused by: No value accessor for form control with name: 'pets'
Error: No value accessor for form control with name: 'pets'
at _throwError (http://localhost:3000/node_modules/@angular/forms/bundles/forms.umd.js:1231:15)
at setUpControl (http://localhost:3000/node_modules/@angular/forms/bundles/forms.umd.js:1171:13)
at FormGroupDirective.addControl (http://localhost:3000/node_modules/@angular/forms/bundles/forms.umd.js:3595:13)
at FormControlName._setUpControl (http://localhost:3000/node_modules/@angular/forms/bundles/forms.umd.js:4029:48)
at FormControlName.ngOnChanges (http://localhost:3000/node_modules/@angular/forms/bundles/forms.umd.js:3975:22)
at Wrapper_FormControlName.detectChangesInInputProps (/ReactiveFormsModule/FormControlName/wrapper.ngfactory.js:44:18)
at _View_PostApartmentComponent0.detectChangesInternal (/AppModule/PostApartmentComponent/component.ngfactory.js:4994:30)
at _View_PostApartmentComponent0.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9305:18)
at _View_PostApartmentComponent0.DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9410:48)
at _View_PostApartmentComponent_Host0.AppView.detectViewChildrenChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9331:23)
相关代码如下:
HTML 表单组件的视图
<form [formGroup]="registerApartment4RentForm" (submit)="onSubmit($event)">
<p id="petsPolicy" class="required" aria-labelledby="petsPolicy"
i18n="select summary pets policy">Pets Policy - Summarized</p>
<md-radio-group>
<md-radio-button i18n="No pets allowed" value="no_pets" formControlName="pets">No pets allowed</md-radio-button>
<md-radio-button i18n="Cats allowed" value="cats_allowed" formControlName="pets">Cats allowed</md-radio-button>
</md-radio-group>
用于管理表单的组件
import { Component } from "@angular/core";
import { FormBuilder, FormGroup, FormControl } from '@angular/forms';
@Component({ ...etc.})
export class PostApartmentComponent {
registerApartment4RentForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.registerApartment4RentForm = this.formBuilder.group({
city: '',
zipCode: '',
streetAddress: '',
pets: ''
});
}
}
form = new FormGroup({
pets: new FormControl( )
});
}
我尝试导入FormControlName,但它没有帮助。除了错误声明之外,单选按钮不会在单击新按钮时切换,按钮圆圈并排显示为2,而不是1在另一个内部。此外,所有md输入都不显示。什么时候
formControlName="pets"
从md-ratio-button HTML中删除,md-inputs重新出现并正常工作。
我已经从devdocs中复制代码以获取被动表单并更改了变量名称。还有什么办法可以让md-radio-buttons工作?
答案 0 :(得分:2)
我遇到了同样的问题,直到我发现你必须在formControlName
组件上设置md-radio-group
:
<form novalidate [formGroup]="userInfoForm" (ngSubmit)="onSubmit()">
<md-radio-group name="gender" formControlName="gender">
<md-radio-button class="example-radio-button" value="Female">
Female
</md-radio-button>
<md-radio-button class="example-radio-button" value="Male">
Male
</md-radio-button>
</md-radio-group>
</form>