我按照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工作?
解决方法:
我遇到了同样的问题,直到我发现你必须在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>
原文地址:https://codeday.me/bug/20190701/1352143.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。