我正在按照 http://devdocs.io/angular~2_typescript/api/forms/index/radiocontrolvalueaccessor-directive 上关于响应式(Reactive)单选按钮的说明进行操作并使用 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 用于表单组件的 View
<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 中复制了 react 形式的代码并更改了变量名。要让 md-radio-buttons 正常工作还需要做什么?
最佳答案
我遇到了同样的问题,直到我发现,您必须在 md-radio-group
组件上设置 formControlName
:
<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>
关于javascript - 响应式(Reactive)(基于模型)形式的 Angular 2 md-radio 按钮不起作用并停止 md-input 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40597983/