【问题标题】:here is no directive with "exportAs" set to "ngForm"这里没有将“exportAs”设置为“ngForm”的指令
【发布时间】:2017-09-10 09:21:18
【问题描述】:

我正在使用以下代码在 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
    ],

【问题讨论】:

标签: angular typescript


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你必须关心的两件事..

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

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

          **exports:[FormsModule],**
      

      所以在一起看起来像 导入:[CommonModule,HttpModule,FormsModule], 出口:[FormsModule],

    3. 在顶部你必须导入 FormsModule

      从“@angular/forms”导入 {FormsModule};


    如果你只使用 app.module.ts 那么

    不需要导出..

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 2017-11-08
      • 2018-05-12
      • 2018-01-16
      • 2016-12-03
      • 2018-10-20
      • 2018-11-20
      • 2017-01-26
      相关资源
      最近更新 更多