问题描述
我的想法已经用完了。我想在角度库的组件中的输入中使用 [(ngModel)],因此我需要在库的模块中导入 FormsModule 才能使用它。我做的。我有一个演示项目,我在其中调用该库的一个组件来测试它,但在标题中出现错误。有人知道该怎么做吗?
我还在演示 angular 项目中的一个组件中尝试了在 app.module 中导入 FormsModule 并且它工作正常。但是在库的组件中是不可能实现的。
错误:
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)
有人有什么想法吗?提前致谢。
库的 .module 文件:
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,})
export class CalculatorsLibraryModule { }
project.ts 文件:
/*
* 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';
demo angular 项目的app.module:
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:
<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>
解决方法
您有导出组件的库模块,但没有导出所需的模块,因此有两个选项:
- 在库模块中导出
FormsModule
- 在 AppModule 中导入
FormsModule
。
我发现了错误。我在另一个组件中命名了一个属性,其名称与 ReactiveForms 中的指令使用的名称相同。好像在我导入 FormsModule 时,错误出现而不是在此操作之前,所以我也需要导入 ReactiveForms 来解决问题。 就我而言,我不需要 ReactiveForms,因此从其他组件中删除属性并重命名它解决了问题,现在一切正常。