TypeScript-'s Angular Framework Error - “There is no directive with exportAs set to ngForm”

前端 未结 22 1448
轻奢々
轻奢々 2020-12-04 09:18

I keep getting this error while using TypeScript\'s Angular2-forms framework:

There is no directive with \"exportAs\" set to \"ngForm

相关标签:
22条回答
  • 2020-12-04 09:38

    Two Things you have to care..

    1. If you using the sub module, you have to import the FormModule in that sub module.

              **imports:[CommonModule,HttpModule,FormsModule]**
      
    2. you have to exports the FormModule in the module

          **exports:[FormsModule],**
      

      so together it looks like imports:[CommonModule,HttpModule,FormsModule], exports:[FormsModule],

    3. in Top u have to import the FormsModule

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


    if you are using only the app.module.ts then

    no need to export.. only import required

    0 讨论(0)
  • 2020-12-04 09:38

    I just moved routing modules i.e. say ARoutingModule above FormsModule and ReactiveFormsModule and after CommonModule in imports array of modules.

    0 讨论(0)
  • 2020-12-04 09:39
    import { FormsModule }   from '@angular/forms';
    
    @NgModule({
      imports: [FormsModule],
      ...
    })
    
    0 讨论(0)
  • 2020-12-04 09:39

    (Just in case someone else is blind like me) form FTW! Make sure to use <form> tag

    wont work:

    <div (ngSubmit)="search()" #f="ngForm" class="input-group">
        <span class="input-group-btn">
          <button class="btn btn-secondary" type="submit">Go!</button>
        </span>
        <input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search...">
    </div>
    

    works like charm:

     <form (ngSubmit)="search()" #f="ngForm" class="input-group">
                <span class="input-group-btn">
                  <button class="btn btn-secondary" type="submit">Go!</button>
                </span>
                <input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search...">
    </form>
    
    0 讨论(0)
  • 2020-12-04 09:39

    You must import the FormsModule and then place it in the section of imports.

    import { FormsModule } from '@angular/forms';
    
    0 讨论(0)
  • 2020-12-04 09:39

    Simple if you have not import module then import and declare import { FormsModule } from '@angular/forms';

    and if you did then you just need to remove formControlName='whatever' from input fields.

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