Angular2 Error: There is no directive with “exportAs” set to “ngForm”

后端 未结 11 2385
小鲜肉
小鲜肉 2020-12-01 13:33

i\'m on the RC4 and i\'m getting the error There is no directive with \"exportAs\" set to \"ngForm\" because of my template :

相关标签:
11条回答
  • 2020-12-01 14:12

    If you are getting this instead:

    Error: Template parse errors:

    There is no directive with "exportAs" set to "ngModel"

    Which was reported as a bug in github, then likely it is not a bug since you might:

    1. have a syntax error (e.g. an extra bracket: [(ngModel)]]=), OR
    2. be mixing Reactive forms directives, such as formControlName, with the ngModel directive. This "has been deprecated in Angular v6 and will be removed in Angular v7", since this mixes both form strategies, making it:
    • seem like the actual ngModel directive is being used, but in fact it's an input/output property named ngModel on the reactive form directive that simply approximates (some of) its behavior. Specifically, it allows getting/setting the value and intercepting value events. However, some of ngModel's other features - like delaying updates withngModelOptions or exporting the directive - simply don't work (...)

    • this pattern mixes template-driven and reactive forms strategies, which we generally don't recommend because it doesn't take advantage of the full benefits of either strategy. (...)

    • To update your code before v7, you'll want to decide whether to stick with reactive form directives (and get/set values using reactive forms patterns) or switch over to template-driven directives.

    When you have an input like this:

    <input formControlName="first" [(ngModel)]="value">
    

    It will show a warning about mixed form strategies in the browser's console:

    It looks like you're using ngModel on the same form field as formControlName.

    However, if you add the ngModel as a value in a reference variable, example:

    <input formControlName="first" #firstIn="ngModel" [(ngModel)]="value">
    

    The error above is then triggered and no warning about strategy mixing is shown.

    0 讨论(0)
  • 2020-12-01 14:13

    Also realized this problem comes up when trying to combine reactive form and template form approaches. I had #name="ngModel" and [formControl]="name" on the same element. Removing either one fixed the issue. Also not that if you use #name=ngModel you should also have a property such as this [(ngModel)]="name" , otherwise, You will still get the errors. This applies to angular 6, 7 and 8 too.

    0 讨论(0)
  • 2020-12-01 14:15

    I faced the same issue. I had missed the forms module import tag in the app.module.ts

    import { FormsModule } from '@angular/forms';
    
    @NgModule({
        imports: [BrowserModule,
            FormsModule
        ],
    
    0 讨论(0)
  • 2020-12-01 14:15

    I had this problem because I had a typo in my template near [(ngModel)]]. Extra bracket. Example:

    <input id="descr" name="descr" type="text" required class="form-control width-half"
          [ngClass]="{'is-invalid': descr.dirty && !descr.valid}" maxlength="16" [(ngModel)]]="category.descr"
          [disabled]="isDescrReadOnly" #descr="ngModel">
    
    0 讨论(0)
  • 2020-12-01 14:18

    I had this problem and I realized I had not bound my component to a variable.

    Changed

    <input #myComponent="ngModel" />

    to

    <input #myComponent="ngModel" [(ngModel)]="myvar" />

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