2

My angular application run smoothly most of the times but it sometime fail at startup with messages like

Can't bind to 'ngIf' since it isn't a known property of 'ng-container'.

As already stated at Can't bind to 'ngIf' since it isn't a known property of 'div' the issue is that the CommonModule is not imported. I'm probably doing something wrong but it seems the CommonModule is imported (if not it would never work, else it's working 90% of the time).

Here is my configuration:

app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [
    CommonModule,
    AppRoutingModule,
    NavigationModule,
    HttpClientModule,
    BrowserModule,
    BrowserAnimationsModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    }),
    AngularSvgIconModule.forRoot()
  ],
  providers: [
    {
      // tslint:disable-next-line: max-line-length
      // Inspired by https://www.mokkapps.de/blog/how-to-build-an-angular-app-once-and-deploy-it-to-multiple-environments/#solution-3-mount-configuration-files-from-environment
      // Load configuration file.
      provide: APP_INITIALIZER,
      useFactory: (envConfigService: EnvironmentService) => async () => envConfigService.loadConfiguration(),
      deps: [EnvironmentService],
      multi: true
    },
    CookieService,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: CacheInterceptor,
      multi: true
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MyHttpInterceptor,
      multi: true
    },
    I18n,
    Xliff,
    { provide: TRANSLATIONS, useValue: translationsEn },
    { provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
    { provide: MatPaginatorIntl, useClass: CustomMatPaginatorIntl },
    CookieService,
    OverlayModule,
    DeviceDetectorUtils,
    MatDatepickerModule,
    MatNativeDateModule,
    { provide: LOCALE_ID, useValue: 'fr-FR' },
    { provide: DateAdapter, useClass: MomentDateAdapter },
    { provide: MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMAT },
    { provide: MAT_DATE_LOCALE, useValue: 'fr-FR' }
  ],
  bootstrap: [AppComponent]
})

export class AppModule {

}

app-routing-module.ts

All the application is on a single route (/home) which dynamically update components.

const routes: Routes = [
  { path: 'home', component: HomeComponent, loadChildren:  () => {
    return import('./features/feature1/feature1.module').then(m => m.Feature1Module),
           import('./features/feature2/feature2.module').then(m => m.Feature2Module),
           //...
           import('./navigation/navigation.module').then(m => m.NavigationModule),
           import('./features/home/home.module').then(m => m.HomeModule);
  }},
  { path: 'password-creation/:m/:p', component: PasswordCreationComponent , loadChildren:  () => {
    return import('./features/user-management/user-management.module').then(m => m.UserManagementModule);
  }},
  { path: '**', redirectTo: 'home', pathMatch: 'prefix' }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

home.module.ts

@NgModule({
  declarations: [HomeComponent],
  imports: [
    CommonModule, SharedModule, HomeRoutingModule
  ]
})
export class HomeModule { }

home-routing-module.ts

const routes: Routes = [
    {
        path: '',
        component: HomeComponent
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})

export class HomeRoutingModule {}

shared.module.ts

const PIPES = [
  // All my shared pipes
];

const COMPONENTS = [
  // All my shared components
];

const DIRECTIVES = [
  // All my shared directives
];

const MODULES = [
  CommonModule,
  PortalModule,
  ReactiveFormsModule,
  FormsModule,
  LayoutModule,
  RouterModule,
  // All my shared modules
];

const PROVIDERS = [
  // All my shared providers
];

@NgModule({
  declarations: [...PIPES, ...COMPONENTS, ...DIRECTIVES],
  imports: [...MODULES],
  exports: [...PIPES, ...COMPONENTS, ...DIRECTIVES, ...MODULES],
  providers: [...PROVIDERS]
})
export class SharedModule { }

Every feature module imports the sharedModule.

I'm guessing this is due to lazy loading but the CommonModule and BrowserModule are imported in the AppModule so they should not be lazy loaded, right?

ng version

Angular CLI: 9.1.15
Node: 14.16.1      
OS: win32 x64

Angular: 9.1.13
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.901.15
@angular-devkit/build-angular      0.901.15
@angular-devkit/build-optimizer    0.901.15
@angular-devkit/build-webpack      0.901.15
@angular-devkit/core               9.1.15
@angular-devkit/schematics         9.1.15
@angular/cdk                       9.2.4
@angular/cli                       9.1.15
@angular/material                  9.2.4
@angular/material-moment-adapter   11.2.13
@ngtools/webpack                   9.1.15
@schematics/angular                9.1.15
@schematics/update                 0.901.15
rxjs                               6.5.5
typescript                         3.8.3
webpack                            4.42.0

Thanks for any insights.

MHogge
  • 5,408
  • 15
  • 61
  • 104
  • 1
    Have you tried checking that a) You've included the correct spelling/syntax of ngIf and b) You've restarted your server before making that change to your module? Every time I encounter this issue I suspect it's some fundamental then realise I've written "ngif" isntead of "ngIf" – Alex Jun 18 '21 at 08:45
  • a) Yes I've checked. And as I said the application run smoothly most of the times, if it was a syntax error it would always fail. b) Yes I tried and it's not the issue. The issue may disappear after a restart but it also may not. In order to make the issue disappear I can refresh the browser page (sometime I need to refresh it more than once until the issue is gone). – MHogge Jun 18 '21 at 08:54
  • @MHogge is this an Angular 12 app? Very similar thing started happening to me when I transitioned from Angular 8 to Angular 12. – mutantkeyboard Jun 18 '21 at 08:54
  • @mutantkeyboard No I'm on version 9.1.15. Did you find a solution to your issue ? – MHogge Jun 18 '21 at 08:56
  • @MHogge - I actually deleted the whole `@angular/cli` and removed the`node_modules` and did a fresh install. I never figured out what might be a cause of that issue. – mutantkeyboard Jun 18 '21 at 08:59
  • Does your CLI version match your core version? – Alex Jun 18 '21 at 09:02
  • @Alex No. And I previously gives the wrong Angular version. I update the question with the full output of `ng version`. – MHogge Jun 18 '21 at 09:22

0 Answers0