2

I've followed the example in the documentation, and get "Error: No provider for ElementRef!". It works fine when using ElementRef in an @Component. All I'm adding is the ElementRef and Renderer (renderer does not have a problem):

import { Directive, AfterViewInit, ElementRef, Renderer} from '@angular/core';
export class blah implements AfterViewInit {

constructor(private el: ElementRef, private renderer: Renderer) { }

ngAfterViewInit() {
    ...

Nothing I tried worked at all, so I took the example from the documentation, https://angular.io/docs/ts/latest/guide/attribute-directives.html, and put it in the quickstart (with systemJS). It worked fine. When I migrate that to the webpack configuration https://angular.io/docs/ts/latest/guide/webpack.html, it worked fine. The angular2 application that has the issue is much more complex, but I don't know what would cause this.

Stephen
  • 1,603
  • 16
  • 19
  • Possible duplicate of [Injecting ElementRef to injecable error](https://stackoverflow.com/questions/36495706/injecting-elementref-to-injecable-error) – J K Jul 01 '18 at 23:37

1 Answers1

1

As I typed this in, I came across my error. At some point I had added this directive to the providers in "@NgModule". Services cannot use "ElementRef", and thus @Directives not be treated as services. Still, it is an odd error.

Stephen
  • 1,603
  • 16
  • 19