将ReactiveFormsModule添加到我的angular 4应用程序后,没有获得NgControl错误的提供程序

Jos*_*ebi 12 angular

我收到此错误 - "模板解析错误:NgControl没有提供者"

 The error comes from this line of code

 --> <select (ngModel)="currencies" (ngModelChange)="showPurchase($event)" class="annka-center" name="curencies">
       <option *ngFor="let money of currencies; let i = index" (ngValue)="money" (click)="showPurchase(money)"> {{money.Currency}} </option>
 </select>
Run Code Online (Sandbox Code Playgroud)

上面的代码工作顺利,直到我将ReactiveFormsModule添加到我的应用程序.

我在这里尝试了解决方案

错误:没有NgControl Angular AOT的提供者

但这对我没有用.我正在使用Angular 4.

Saj*_*ran 15

应该

<select [(ngModel)]="currencies" (ngModelChange)="showPurchase($event)" class="annka-center" name="curencies">
       <option *ngFor="let money of currencies; let i = index" (ngValue)="money" (click)="showPurchase(money)"> {{money.Currency}} </option>
 </select>
Run Code Online (Sandbox Code Playgroud)

还要确保从'@ angular/forms'导入导入{FormsModule,ReactiveFormsModule}下的app.module.ts中导入FormsModule;

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

    @NgModule({
        imports: [
             FormsModule      
        ]
Run Code Online (Sandbox Code Playgroud)

  • 只是为了澄清,答案还应该在导入数组中包含“ReactiveFormsModule”。 (8认同)

Vik*_*ngh 6

我收到此错误是因为我忘记添加[(ngModel)]输入元素。

<input [(ngModel)]="firstName/>
Run Code Online (Sandbox Code Playgroud)


Ste*_*aul 5

我也收到此错误,因为我忘记将formControlName指令添加到模板中的表单控件中,这意味着:

<input formControlName="firstName" />
Run Code Online (Sandbox Code Playgroud)