ng-packagr: Error StaticInjectorError[ViewContainerRef] when having *ngIf or *ngFor // Solution: `--preserve-symlinks`

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

When I am using either *ngIf or *ngFor in my template I got this error message: StaticInjectorError[ViewContainerRef]. Any ideas why this happen?

screen shot 2017-11-22 at 5 48 52 pm

How To Reproduce

  1. Create a component footer.component.ts
  2. Add *ngIf in footer.component.html
  3. Run ng-packagr -p ng-package.json and npm publish
  4. Install on new Angular 5 app (npm install)
  5. Run my Angular 5 App ng serve

Here is my code:

footer.component.ts

import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {
  @Input() isTrue: boolean;

  constructor( ) { }

  ngOnInit() { }
}

footer.component.html

<footer>
    <h2 *ngIf="isTrue">Hello</h2>
</footer>

Expected Behaviour

Should not error

Version Information

ng-packagr: ^1.6.0
node: v7.6.0
@angular: v5.0.2
rxjs: v5.5.2
zone.js: v0.8.18

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25 (5 by maintainers)

Most upvoted comments

Still facing with StaticInjectionError even with --preserve-symlinks flag. Unfortunately it’s not the solution for *ngIf and *ngFor case.

Have you tried the --preserve-symlinks flag?

Hi all,

please check and confirm whether --preserve-symlinks solves the issue as it does for the original error (StaticInjectorError). Should it not in your case, please open a separate issue.

As mentioned in https://github.com/dominique-mueller/angular-package-builder/issues/38, using the --aot or --preserve-symlinks flag on ng serve probably solves this issue (which occurs when installing the library via a local directory instead of remotely).

UPDATE: I manage to solve the issue by updating both my lib and my app that uses the lib to Angular 5.2.8 (it works with 5.2.9 also) and angular cli to 1.7.3. No preserveSymlinks or any other configuration is needed!

@vapits Could you link an example project with this working? I’m currently still having issues using the Angular 5.2.8 and cli 1.7.3. Maybe there is another package that needs to be a specific version or you have a specific angular/webpack config? My main app will not dynamically load the lib with *ngIf, but loads fine without an *ngIf.

i dont whats it getting wrong but for me following solution works (i facing this in anguiar5) add in angular-cli.json “defaults”: { “styleExt”: “css”, “component”: {}, “build”: { “preserveSymlinks”: true } }

this may help … 😃

@doenikoe did you get this issue fixed?