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

前端 未结 22 1447
轻奢々
轻奢々 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:30
    import { FormsModule,ReactiveFormsModule }from'@angular/forms';
    
    imports:[
        BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
        HttpClientModule,
        FormsModule,
        ReactiveFormsModule/*This one in particular*/,...
    ],
    

    in app.module.ts to permanently solve errors like "cannot bind [formGroup] or no directive with export as".

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

    In case a name is assigned like this:

    #editForm="testForm"
    

    ... the exportAs has to be defined in the component decorator:

    selector: 'test-form',
    templateUrl: './test-form.component.html',
    styleUrls: ['./test-form.component.scss'],
    exportAs: 'testForm'
    
    0 讨论(0)
  • 2020-12-04 09:32

    check whether you import FormsModule. There's no ngControl in the new forms angular 2 release version. you have to change your template to as an example

    <div class="row">
        <div class="form-group col-sm-7 col-md-5">
            <label for="name">Name</label>
            <input type="text" class="form-control" required
                   [(ngModel)]="user.name"
                   name="name" #name="ngModel">
            <div [hidden]="name.valid || name.pristine" class="alert alert-danger">
                Name is required
            </div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-04 09:33

    I've come to this same question over & over again, also due to same reason. So let me answer this by saying what wrong I was doing. Might be helpful for someone.

    I was creating component via angular-cli by command

    ng g c components/something/something

    What it did, was created the component as I wanted.

    Also, While creating the component, it added the component in the App Module's declarations array.

    If this is the case, please remove it. And Voila! It might work.

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

    You have to import FormsModule into not only the root AppModule, but also into every subModule that uses any angular forms directives.

    // SubModule A
    
    import { CommonModule } from '@angular/common';
    import { FormsModule }   from '@angular/forms';
    
    @NgModule({
      imports: [
        CommonModule,
        FormsModule      //<----------make sure you have added this.
      ],
      ....
    })
    
    0 讨论(0)
  • 2020-12-04 09:36

    This

    <div #myForm="ngForm"></div>
    

    Also causes the error and can be fixed by including the ngForm directive.

    <div ngForm #myForm="ngForm"></div>
    
    0 讨论(0)
提交回复
热议问题