Angular - 没有将 "exportAs" 设置为 "ngModel" 的指令

Angular - There is no directive with "exportAs" set to "ngModel"

以下是 AngularJS 项目中的文件。正如一些帖子中所建议的,我添加了:

ngModel name="currentPassword" #currentPassword="ngModel 

在输入字段中,但仍然出现错误。

package.json

.........
"dependencies": {
        "@angular/common": "^2.3.1",
        "@angular/compiler": "^2.3.1",
        "@angular/core": "^2.3.1",
        "@angular/forms": "^2.3.1",
        "@angular/http": "^2.3.1",
        "@angular/platform-browser": "^2.3.1",
        "@angular/platform-browser-dynamic": "^2.3.1",
        "@angular/router": "^3.3.1",
        "core-js": "^2.4.1",
        "rxjs": "^5.0.1",
        "ts-helpers": "^1.1.1",
        "zone.js": "^0.7.2"
    },
   ...............

变化-password.component.html

<form #f="ngForm" [formGroup]="changePasswordForm">
        <div class="form-group">
            <label for="currentPassword">Current Password</label> <input
                placeholder="currentPassword" ngModel name="currentPassword"
                #currentPassword="ngModel" id="currentPassword"
                name="currentPassword" type="text" class="form-control" required
                minlength="6" formControlName="currentPassword">
            </div>
        </div>
        <button class="btn btn-primary" type="submit">Change Password</button>
    </form>

变化-password.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators, ControlContainer, FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-change-password',
  templateUrl: './change-password.component.html',
  styleUrls: ['./change-password.component.css']
})
export class ChangePasswordComponent implements OnInit {

  changePasswordForm;

  constructor(formBuilder: FormBuilder) {
    this.changePasswordForm = formBuilder.group({
      currentPassword: new FormControl('', Validators.compose([Validators.required]))
    });
  }

  ngOnInit() {
  }

}

app.module.ts 有导入

............
imports: [
    BrowserModule,
    HttpModule,
    FormsModule,
    ReactiveFormsModule
  ]
...........

我收到以下错误:

Unhandled Promise rejection: Template parse errors: There is no directive with "exportAs" set to "ngModel" ("urrent Password ]#currentPassword="ngModel" id="currentPassword" name="currentPassword" type="text" class="form-co"): ChangePasswordComponent@5:4 ; Zone: ; Task: Promise.then ; Value: SyntaxError {__zone_symbol__error: Error: Template parse errors: There is no directive with "exportAs" set to "ngModel" ("urrent Passwo……} Error: Template parse errors: There is no directive with "exportAs" set to "ngModel" ("urrent Password ]#currentPassword="ngModel" id="currentPassword" name="currentPassword" type="text" class="form-co"): ChangePasswordComponent@5:4 at SyntaxError.ZoneAwareError (http://localhost:4200/polyfills.bundle.js:6884:33) at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:64475:16) at new SyntaxError (http://localhost:4200/vendor.bundle.js:5727:16)

您使用的是模板驱动和模型驱动表单的奇怪组合。选择其中一个,不要混合这两个。所以这是模型驱动形式的示例:

无需在模板中设置requiredminlength,我们在组件中处理。此外,我们不需要任何 ngModelname 等,因为我们使用 formControlName。还要删除 #f="ngForm",因为它与模板驱动的表单相关。所以你的模板应该是这样的:

<form [formGroup]="changePasswordForm">
  <label for="currentPassword">Current Password</label> 
  <input formControlName="currentPassword">
  <button type="submit">Change Password</button>
</form>

并且在构建表单时,我们设置了我们需要的所有验证器,在本例中为 requiredminLength:

constructor(formBuilder: FormBuilder) {
  this.changePasswordForm = formBuilder.group({
    currentPassword: new FormControl('', 
          Validators.compose([Validators.required, Validators.minLength(6)]))
  });
}

就是这样! :)

我建议你阅读表格,这里是 the guide for template driven forms and guide for reactive forms

编辑:

对于表单验证,勾选official docs进行表单验证。如果您有很多字段,您可能需要调整他们的解决方案,将所有表单错误存储在一个单独的对象中。

但这是检查每个表单控件的表单错误的基本解决方案。因此,以下将用于您的验证:

<small *ngIf="changePasswordForm.get('currentPassword').hasError('required')">Required!</small>
<small *ngIf="changePasswordForm.get('currentPassword').hasError('minlength')">Minimum 6 chars</small>

您可能还想在触摸字段时显示错误消息 (??)。您可以在我提供的用于验证的 link 中找到所有这些:)

Updated Demo

添加 import { FormsModule } from '@angular/forms';到您的 app.module.ts 并在您需要添加 FormsModule 的导入数组中。

答案已经很晚了,但如果有人在 angular 5 中遇到问题,你需要这样做。

这可能会对 Angular 6 中的某个人有所帮助。

我忘记将 ngModel 指令添加到我的输入控件中,但已将 #currentPassword="ngModel" 添加到我的表单中。进口等都到位了。

如果您使用 angular 模板驱动表单并希望使用 #xname="ngModel",您还需要在同一输入中使用 [(ngModel)]="mymodel" 指令,当然,还需要导入 de FormsModule 到您的 app.module,如上面其他答案所述

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ContactFormComponent } from './contact-form/contact-form.component';

@NgModule({
  declarations: [
    AppComponent,
    ContactFormComponent
  ],
  imports: [
    FormsModule,
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

导入 FormsModule 解决了问题

可能有两个原因:

  1. 检查您是否在 app.module.ts 及其导入下缺少 FormModule(下图),导入:[]

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

  2. 如果正确保存所有内容后问题仍然存在。停止服务器并重新运行 ng server