[Fixed] No provider for NgControl found in NodeInjector when using angular library

Issue

I have run out of ideas. I want to use [(ngModel)] in my inputs in a component of an angular library so I need to import FormsModule in the library’s module to use it. I did it. I have a demo project where I call a component of this library to test it but I get the error in the title. Someone knows what to do?

I tried also in a component in the demo angular project importing FormsModule in app.module and it works. But it is imposible to achieve in the component of the library.

Error:

core.js:6162 ERROR Error: NG0201: No provider for NgControl found in NodeInjector. Find more at https://angular.io/errors/NG0201
    at throwProviderNotFoundError (core.js:233)
    at notFoundValueOrThrow (core.js:3316)
    at lookupTokenUsingModuleInjector (core.js:3351)
    at getOrCreateInjectable (core.js:3453)
    at Module.ɵɵdirectiveInject (core.js:14634)
    at NodeInjectorFactory.NgControlStatus_Factory [as factory] (forms.js:1301)
    at getNodeInjectable (core.js:3548)
    at instantiateAllDirectives (core.js:10227)
    at createDirectivesInstances (core.js:9576)
    at Module.ɵɵelementStart (core.js:14778)

Has someone any ideas? Thanks in advance.

library´s .module file:

import { NgModule } from '@angular/core';
import { SavingGoalComponent } from './saving-goal/saving-goal.component';
import { EarlyRetirementComponent } from './early-retirement/early-retirement.component';
import { SavingsDurationComponent } from './savings-duration/savings-duration.component';
import { MatSliderModule } from '@angular/material/slider';
import { MatSelectModule } from '@angular/material/select';
import { MatFormFieldModule } from '@angular/material/form-field';
import { CommonModule } from '@angular/common';
import { TranslateModule } from '@ngx-translate/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

@NgModule({
  declarations: [
    SavingGoalComponent,
    EarlyRetirementComponent,
    SavingsDurationComponent
  ],
  imports: [
    CommonModule,
    BrowserModule,
    TranslateModule,
    MatSliderModule,
    MatSelectModule,
    MatFormFieldModule,
    FormsModule
  ],
  exports: [
    SavingGoalComponent,
    EarlyRetirementComponent,
    SavingsDurationComponent
  ],
})
export class CalculatorsLibraryModule { }

project.ts file:

/*
 * Public API Surface of calculators-library
 */

export * from './lib/calculators-library.module';

export { SavingGoalComponent } from './lib/saving-goal/saving-goal.component';
export { EarlyRetirementComponent } from './lib/early-retirement/early-retirement.component';
export { SavingsDurationComponent } from './lib/savings-duration/savings-duration.component';

app.module of demo angular project:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CalculatorsLibraryModule } from '../../../calculators-library/src/project';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (http: HttpClient) => {
          return new TranslateHttpLoader(
            http,
            '../../assets/i18n/'
          );
        },
        deps: [HttpClient]
      },
      defaultLanguage: 'es'
    }),
    AppRoutingModule,
    CalculatorsLibraryModule,
    BrowserAnimationsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})`enter code here`
export class AppModule { }

app.component of demo project:

<div class="calculator">
    <lib-early-retirement></lib-early-retirement>
</div>

lib component.html:

<div class="module-column">
                <label for="erc-age">Tu edad actual es</label>
                <input id="erc-age" name="erc-age" type="text" [(ngModel)]="inputs.Age">
                <mat-slider [(ngModel)]="inputs.Age" [min]="0" [max]="100"></mat-slider>
            </div>

Solution

I found the error. I named an atributte in another component with the same name that it is used by a directive in ReactiveForms. It seems like when I import FormsModule, the error appears and not before this action, so I needed to import ReactiveForms too to solve the problem.
In my case, I do not need ReactiveForms, so removing the atributte from the other component and renaming it solve the problem and now everything works fine.

Leave a Reply

(*) Required, Your email will not be published