1

I am creating form validation in angular and I am getting error

No directive found with exportAs 'ngModel'.

My code:

<form>
  <div class="form-group">
      <label for="firstname">First Name</label>
      <input type="text" id="name" name="name" required minlength="4" 
                     appForbiddenName="bob" ngModel #name="ngModel">
                     
      <div class="alert alert-danger" *ngIf>First name required</div>
  </div>
  <div class="form-group">
      <label for="comment">Comment</label>
      <textarea name="" id="comment" cols="30" rows="10" class="form-control"></textarea>
  </div>
  <button class="btn btn-primary">Submit</button>
</form>

image of editor

The error:

     Error: src/app/app.component.html:6:60 - error NG8003: No directive found with exportAs 'ngModel'.
    
    6                      appForbiddenName="bob" ngModel #name="ngModel">
                                                                 ~~~~~~~
    
      src/app/app.component.ts:5:16
        5   templateUrl: './app.component.html',
                         ~~~~~~~~~~~~~~~~~~~~~~

Error occurs in the template of component AppComponent.

Giannis
  • 1,790
  • 1
  • 11
  • 29
Aniket Thorat
  • 107
  • 2
  • 10

3 Answers3

7

If you work with angular template-driven forms and want to use #name="ngModel" you also need to use [(ngModel)]="mymodel" directive in the same input, and of course,

Add import { FormsModule } from '@angular/forms';

to your app.module.ts and in the import array you need to add FormsModule.

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
Abru007
  • 435
  • 4
  • 12
1

Whenever you get such error make sure you have imported the forms module in the main module.

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; // <== add the imports!
 
import { AppComponent }  from './app.component';
 
@NgModule({
  imports: [
    BrowserModule,
    FormsModule,                               
    ReactiveFormsModule                       
  ],
  declarations: [
    AppComponent
    // other components here
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Check here for more details.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
1

I was migrating an Angular 8 application over to an Angular 13 UI Kit that I purchased. I ran into this issue. I was getting the exportAs 'ngModel' and 'ngForm' error. It turns out that I missed adding one of my Tab pages to the app-module. I spent hours trying to figure this out. Had nothing to do with adding the FormsModule.

Dumber_Texan2
  • 840
  • 2
  • 12
  • 34
  • 1
    Correct. I have mentioned a similar solution here: [link]https://stackoverflow.com/questions/44106910/angular-there-is-no-directive-with-exportas-set-to-ngmodel/75831106#75831106 – Shubhjot Mar 24 '23 at 09:06