ngModel: No value accessor for ''

后端 未结 10 2264
暖寄归人
暖寄归人 2020-12-09 15:08

I\'m creating a custom element in Angular 2.0 (), and when I include the ngModel attribute on the component, I\'m immediately hit with the foll

10条回答
  •  忘掉有多难
    2020-12-09 16:04

    I take this error for 2 reasons:

    1) Component has to attach itself as a value accessor, like:

    @Component({
      selector: 'ace-editor',
      template: `
    `, }) export class AceEditor implements OnInit, ControlValueAccessor { constructor(private element: ElementRef, @Optional() ngControl: NgControl) { if (ngControl) { ngControl.valueAccessor = this; } }

    2) You haven't to mix deprecated and new forms. If you use a new API add to your main.ts:

    import { bootstrap }    from '@angular/platform-browser-dynamic';
    import { disableDeprecatedForms, provideForms } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    
    bootstrap(AppComponent, [
      disableDeprecatedForms(),
      provideForms()
    ])
    .catch((err: any) => console.error(err));
    

提交回复
热议问题