ngModel: No value accessor for ''

后端 未结 10 2218
暖寄归人
暖寄归人 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:05

    I was not using new Form APIs, and I was still getting this error for NgModel in my unit test.

    Looks like just specifying NgModel in your component isn't sufficient. e.g.

    Before
    directives:[NgModel]

    After
    directives:[FORM_DIRECTIVES]

    That fixed my problem. Cheers :)

    0 讨论(0)
  • 2020-12-09 16:07

    In my case it was a simple typo error. The component's selector was 'my-component' but in the html I typed <my-compoent>. Notice last three letters? Compiles just fine and then you have this 'no value accessor' error at runtime.

    From my experience you can have some other quite weird errors because of typos here and there. Typescript compiler doesn't care about markup.

    0 讨论(0)
  • 2020-12-09 16:08

    I just got this error as well. And it was my stupidity. I will describe it here in case someone else jumps into it.

    The format of 2 way binding for passing variables between components (let say I want to pass obj called myObj from Component A to Component B).

    So inside the template for Component A I would have something like

    <componentB [(myObjectInB)]="myObjectInA"></componentB>
    

    Inside component B, I have to have an object called myObjectInB and the way we declare it must be:

    @Input() myObjectInB ...
    

    The @Input() here is what I missed so that's why it threw that error.

    0 讨论(0)
  • 2020-12-09 16:11

    Try this

    import {FORM_DIRECTIVES} from '@angular/forms';
    

    Import any and all forms symbols - NgForm, Validators, etc - from @angular/forms. Importing them from @angular/common will result in a No value accessor found error.

    0 讨论(0)
提交回复
热议问题