i\'m on the RC4 and i\'m getting the error There is no directive with \"exportAs\" set to \"ngForm\" because of my template :
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:
[(ngModel)]]=
), ORformControlName
, 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 namedngModel
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 ofngModel
's other features - like delaying updates withngModel
Options 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 asformControlName
.
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.
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.
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
],
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">
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" />