I divided bigger app into modules (feature modules, core module and shared module). I am using Angular Material and therefore I imported BrowserAnimationsModule
. I placed it in Shared Module and everything works fine, but the problem is arising when I want to lazy load some feature modules - then I have error about double import of BrowserModules. I know that BrowserAnimationsModule should be imported by Core Module, but when I am trying to do that, I get following error:
Uncaught Error: Template parse errors:
Can't bind to 'ngForOf' since it isn't a known property of 'mat-option'.
1. If 'mat-option' is an Angular component and it has 'ngForOf' input, then verify that it is part of this module.
2. If 'mat-option' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("' | translate}}" (change)="change()" [(ngModel)]="actualFilter" name="food">
<mat-option [ERROR ->]*ngFor="let filterColumn of displayedColumns" [value]="filterColumn">
{{filterColumn | t"): ng:///ChargesModule/ChargesComponent.html@9:22
Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("COLUMNFILTER' | translate}}" (change)="change()" [(ngModel)]="actualFilter" name="food">
[ERROR ->]<mat-option *ngFor="let filterColumn of displayedColumns" [value]="filterColumn">
{{filt"): ng:///ChargesModule/ChargesComponent.html@9:10
Below I am presenting the modules: app.module.ts:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HomeModule,
ChargesModule,
ModeratorPanelModule,
CoreModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
core.module.ts:
@NgModule({
imports: [
HttpModule,
HttpClientModule,
AppRoutingModule,
SharedModule,
BrowserAnimationsModule
],
declarations: [
SidenavComponent,
HeaderComponent,
ErrorPageComponent,
PageNotFoundComponent,
LoginComponent,
UpdatePanelComponent,
DialogConfirmCancelComponent,
ConfirmMessageComponent,
],
exports: [
HttpModule,
HttpClientModule,
SharedModule,
HeaderComponent,
SidenavComponent,
AppRoutingModule,
BrowserAnimationsModule
],
providers: [
AuthService, AuthGuard, HttpAuthService, DictionariesService,
DictionaryItemFactory, CanDeactivateGuard, { provide: MatPaginatorIntl, useClass: CustomPaginator }
],
entryComponents: [DialogConfirmCancelComponent, ConfirmMessageComponent]
})
export class CoreModule { }
shared.module.ts:
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
imports: [
CommonModule,
MaterialModule,
FlexLayoutModule,
CdkTableModule,
FormsModule,
NgbModule.forRoot(),
TimepickerModule.forRoot(),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
exports: [
NgbModule,
TimepickerModule,
TranslateModule,
CurrenciesComponent,
MaterialModule,
FlexLayoutModule,
CdkTableModule,
FormsModule,
DropDownListComponent,
DictionariesComponent
],
declarations: [
DictionariesComponent,
DropDownListComponent
],
providers: []
})
export class SharedModule { }
In MaterialModule, I have imported all material modules together with CommonModule.
If you came across an error like
then you should be aware that it either:
has to do with undeclared
@Input
;or you have some problem with
@NgModule
configurationSince
ngForOf
is a built-in directive let's analyze what's the problem with your@NgModule
configuration.First of all we need to determine where this error comes from. It's easy to see from the error message:
It should be clear that we're getting error for
ChargesComponent
that is part ofChargesModule
. I believeChargesModule
declaration looks like:charges.module.ts
Now if you haven't read my previous answer Angular 2 Use component from another module i strongly suggest you to do it. Make sure you understand how one component/directive/pipe can be used in other module. If you do then just export
CommonModule
fromSharedModule
, if you are still in doubt then read on...The main rule here is
Since we need to use ngForOf directive within
ChargesComponent
template, that is part of ChargesModule, then this directive should be part of directives in transitive ChargesModule.How those directives are collected?
At first glance we would declare
ngForOf
directive inChargesModule
declarations array butngForOf
is already declared inCommonModule
and as directive has to be a part of only one module we can't do that.Therefore continue looking
ngForOf
directives among imported@NgModules
. Well, we importedSharedModule
Let's see which directives it exports:shared.module.ts
SharedModule
exports all directives that are eitherexported from
@NgModules
This means that if NgbModule has exported directives then they will be added to exported directives of SharedModule.
or just directives included in
exports
arrayAs we don't see here
CommonModule
we can assume we will get the errorCan't bind to 'ngForOf'
To solve it we have to add
CommonModule
to exports array and all should work as expected.The good question is
It's hard to understand without knowing how
BrowserAnimationsModule
looks like. Let's achieve enlightenment by looking at source code:and look at BrowserModule:
It’s immediately obvious that if we export
BrowserAnimationsModule
module fromSharedModule
we also export directives fromCommonModule
since:So after moving
BrowserAnimationModule
toCoreModule
yourSharedModule
doesn't exportngForOf
directive anymore and that's why you're getting the error.Not the answer you're looking for? Browse other questions tagged angular typescript module lazy-loading or ask your own question.
asked
viewed
1,486 times
active
1 year, 5 months ago
Linked
Related
Hot Network Questions