Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Components in library do not work with ngIf/ngFor

Tags:

angular

I have an app I am trying to upgrade to angular 5 and library-tize. I setup ng-packagr so the lib is built and bundled, this is no problem until I try to use one of the components with ngIf or ngFor which give me the error

HeaderComponent.html:13 ERROR Error: StaticInjectorError[ViewContainerRef]: 
  StaticInjectorError[ViewContainerRef]: 
    NullInjectorError: No provider for ViewContainerRef!
    at _NullInjector.get (core.js:993)

at runtime. I have looked around and tried some different things but nothing seems to fix it...

Here is relevant module code for the library.

@NgModule({
imports: [
    CommonModule,
    FormsModule,
    RouterModule,

    /* Bootstrap Imports */
    AccordionModule, 
    AlertModule, 
    ButtonsModule, 
    CarouselModule, 
    CollapseModule, 
    BsDropdownModule, 
    ModalModule, 
    PaginationModule, 
    ProgressbarModule, 
    RatingModule, 
    TabsModule, 
    TimepickerModule, 
    TooltipModule, 
    TypeaheadModule, 
    DatepickerModule,

    ServicesModule,
],
declarations: [
    LayoutComponent,
    HeaderComponent,
    FooterComponent,
    OffsidebarComponent,
    SidebarComponent,
    UserblockComponent,
    OverlayComponent,
    BusyIndicatorComponent
],
exports: [
    LayoutComponent
],
providers: [
    UserblockService
]
})
export class ComponentsModule {}

Here is the HTML where the error is reported.

          <a class="hidden-xs" trigger-resize="" (click)="toggleCollapsedSideabar()" *ngIf="!isCollapsedText()">
            <i class="material-icons">menu</i>
          </a>
like image 322
comwizz2 Avatar asked Sep 06 '25 03:09

comwizz2


2 Answers

You should add BrowserModule to your imports array.

@NgModule({
imports: [
    BrowserModule, <----- add this
    CommonModule,
    FormsModule,
    RouterModule,

More info here.

like image 90
Uğur Dinç Avatar answered Sep 07 '25 21:09

Uğur Dinç


Removing the dependencies from library's package.json before adding the library to the main app was the fix for me.

like image 40
Kyle Conkright Avatar answered Sep 07 '25 21:09

Kyle Conkright