0

In my Angular application, I had implemented the method of manual dependency injection for TypeScript child classes described in this blog. The goal was to eliminate having to inject providers into child classes just to pass them into the parent class. It worked fine at the time.

Sometime later, when I went to use it in a second Angular module, it failed. Searching uncovered that the previous method might (should?) have broken in Angular 7 (see this question), but the first use case continued to function.

I narrowed it down to a specific injection that fails: ChangeDetectorRef (CDR). If I wrap each attempted, manual injection in a try/catch the ones after CDR are injected just as if specified in the constructor.

Is there something different or special about CDR? I can't provide it, since it comes from Angular. Is there something about it having to be tied to the child class?

Ultimately, I want to know if there's some way to make it work with the manual injection system, or if it has to remain as an automatic injection from every child class.

Addendum:

The specific error is

StaticInjectorError(AppModule)[ChildComponent -> ChangeDetectorRef]:
  StaticInjectorError(Platform: core)[ChildComponent -> ChangeDetectorRef]:
    NullInjectorError: No provider for ChangeDetectorRef!
nshew13
  • 3,052
  • 5
  • 25
  • 39
  • I think you stil might be able to add it to the providers array in the AppModule. You definitely need to do that with things like DatePipe if you want to use them inside a component ts file – bgraham Feb 28 '19 at 19:41
  • @bgraham, I think that has worked. I just imported it from `@angular/core` and added it to the app module's `providers` list. Please post an answer so I can give you the credit. – nshew13 Mar 04 '19 at 19:10
  • Scratch that. I'm getting an empty `Object` for the CDR injection. – nshew13 Mar 04 '19 at 19:49
  • Found this: https://github.com/angular/angular/issues/14656 – nshew13 Mar 04 '19 at 19:58

1 Answers1

0

For reasons that aren't documented, ChangeDetectorRef (as well as ElementRef, ViewContainerRef and TemplateRef) can't be manually injected in this way.

See https://github.com/angular/angular/issues/14656

ChangeDetectorRef has to remain injected in the child classes and passed to super().

nshew13
  • 3,052
  • 5
  • 25
  • 39