Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngModel: No value accessor for ''

Tags:

angular

I'm creating a custom element in Angular 2.0 (<my-select>), and when I include the ngModel attribute on the component, I'm immediately hit with the following error:

EXCEPTION: No value accessor for '' in [myModel in App@0:195] 

Here's a plunker: http://plnkr.co/edit/wlkPWcB92YZASCwCnmcw?p=preview

(Open console to view error)

If you simply comment out the following line from src/app.ts, the component will render appropriately:

'[ngModel]="myModel"' 

I've done the following:

  1. Imported {FORM_DIRECTIVES} from 'angular2/common'
  2. Included FORM_DIRECTIVES in the directives portion of the @Component
  3. Initialized myModel

What am I missing here?

like image 892
lux Avatar asked Dec 28 '15 15:12

lux


People also ask

How do you resolve no value accessor for form control with name?

You are adding the formControlName to the label and not the input. Update the other input fields as well. The same issue to me because I applied formControlName for a span tag. Thank you!

What does [( ngModel )] mean?

The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.

Should I create error no value accessor for form control with unspecified name attribute?

When you get the error No value accessor for form control with unspecified name attribute , there are generally two things that possibly has gone wrong: You are using ngModel with a third party control that doesn't register a NG_VALUE_ACCESSOR . In this case you need to use ngDefaultControl on the element.

How do you bind a value to ngModel?

If you have a one-way binding to ngModel with [] syntax, changing the domain model's value in the component class sets the value in the view. If you have a two-way binding with [()] syntax (also known as 'banana-in-a-box syntax'), the value in the UI always syncs back to the domain model in your class.


2 Answers

I take this error for 2 reasons:

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

@Component({   selector: 'ace-editor',   template: `<div class="ng2-ace-editor"></div>`, })  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)); 
like image 139
17 revs, 13 users 59% Avatar answered Sep 23 '22 14:09

17 revs, 13 users 59%


I think you should use something else than ngModel for the parameter of your my-select component... Because it's already used by Angular2.

I made a try with model and it seems better... I don't have the error anymore.

Edit

If you want to handle ngModel at the level of your my-select component, you could have a look at this link: https://github.com/angular/angular/issues/2543.

If you want to implement an ngModel-compliant component, you could have a look at the following links:

  • http://restlet.com/blog/2016/02/17/implementing-angular2-forms-beyond-basics-part-2/. See section: NgModel-compatible component
  • Angular 2 custom form input

Hope it helps you, Thierry

like image 40
Thierry Templier Avatar answered Sep 26 '22 14:09

Thierry Templier