这是我的 Angular 组件:
@Component( { selector: 'input-extra-field', template: ` <div class="form-group" [formGroup]="formGroup" > <switch [attr.title]="field.etiquette" [attr.value]="field.valeur" [(ngModel)]="field.valeur" [formControl]="fieldControl" [attr.id]="name" [attr.disabled]="disabled"> </switch> <error-messages [control]="name"></error-messages> </div> ` } )
这是我的课:
export class SwitchExtraField extends ExtraField { @Input() field: ExtraFormField; @Input() entity: { fields: Object }; @Input() formGroup: FormGroup; constructor( formDir: NgForm ) { super( null, null, formDir ); } get disabled(): string { if ( this.field && !!this.field.saisissable && !this.field.saisissable ) { return 'disabled'; } return null; } }
这是我在编译时遇到的错误:
ERROR Error: No value accessor for form control with unspecified name attribute at _throwError (forms.es5.js:1918) at setUpControl (forms.es5.js:1828) at FormControlDirective.webpackJsonp.../../../forms/@angular/forms.es5.js.FormControlDirective.ngOnChanges (forms.es5.js:4617)
当我将元素开关更改为输入时,它可以工作,因为我知道我对其他组件使用相同的结构并且它工作正常。
name="fieldName" ngDefaultControl我通过将属性添加到携带该属性的元素来修复此错误[(ngModel)]。
name="fieldName" ngDefaultControl
[(ngModel)]