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:
{FORM_DIRECTIVES}
from 'angular2/common'
FORM_DIRECTIVES
in the directives
portion of the @Component
myModel
What am I missing here?
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!
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.
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.
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.
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));
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:
Hope it helps you, Thierry
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With