4

我在http://devdocs.io/angular~2_typescript/api/forms/index/radiocontrolvalueaccessor-directive 和使用 Angular 2.1.2 和谷歌的 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 正常工作,还需要做什么?

4

1 回答 1

2

我遇到了同样的问题,直到我发现你必须formControlNamemd-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>
于 2017-03-02T16:02:01.067 回答