这里没有将“exportAs”设置为“ngForm”的指令

时间:2017-04-14 06:01:35

标签: angular typescript

我使用下面的代码在angularjs 2.0中提交表单,但是得到的错误是导出设置为ngForm和ngModel的指令,但我正在使用。 以下是我的表格:

<form name="form" (ngSubmit)="f.form.valid && register()" #f="ngForm" novalidate>
  <div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
    <label for="firstName">First Name</label>
    <input type="text" class="form-control" name="firstName" [(ngModel)]="model.firstName" #firstName="ngModel"
           required/>
    <div *ngIf="f.submitted && !firstName.valid" class="help-block">First Name is required</div>
  </div>
  <div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
    <label for="lastName">Last Name</label>
    <input type="text" class="form-control" name="lastName" [(ngModel)]="model.lastName" #lastName="ngModel" required/>
    <div *ngIf="f.submitted && !lastName.valid" class="help-block">Last Name is required</div>
  </div>
  <div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
    <label for="username">Username</label>
    <input type="text" class="form-control" name="username" [(ngModel)]="model.username" #username="ngModel" required/>
    <div *ngIf="f.submitted && !username.valid" class="help-block">Username is required</div>
  </div>
  <div class="form-group" [ngClass]="{ 'has-error': f.submitted && !password.valid }">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" [(ngModel)]="model.password" #password="ngModel"
           required/>
    <div *ngIf="f.submitted && !password.valid" class="help-block">Password is required</div>
  </div>
  <div class="form-group">
    <button [disabled]="loading" class="btn btn-primary">Register</button>
  </div>
</form>

我还使用以下代码在app.module.ts中导入FormsModule:

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

@NgModule({
    imports: [BrowserModule,
        FormsModule
    ],

2 个答案:

答案 0 :(得分:7)

我得到了解决方案,实际上我在main module.ts文件中使用了import formModule。

我必须在我的情况下使用特定模块里面的文件夹学生,因为学生我把我的模块名称保存在这里有student.module.ts文件,这里我们需要导入formModule。

答案 1 :(得分:-1)

你需要关心的两件事......

  1. 如果使用自定义模块,则必须在该模块中导入FormModule。

            **imports:[CommonModule,HttpModule,FormsModule]**
    
  2. 您必须在该模块中导出FormModule

        **exports:[FormsModule],**
    

    所以它看起来像          的进口:[CommonModule,HTTP模块,FormsModule]          出口:[FormsModule]

  3. 顶部的
  4. 你必须导入FormsModule

    从'@ angular / forms'导入{FormsModule};

  5. 如果您只使用app.module.ts,那么

    无需导出..