question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullInjectorError: No provider for ElementRef!

See original GitHub issue

When I’m simply trying to put el: ElementRef in my directive constructor and use it in my component, I get the following error:

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[SampleDirective -> ElementRef]: 
  StaticInjectorError(Platform: core)[SampleDirective -> ElementRef]: 
    NullInjectorError: No provider for ElementRef!
Error: StaticInjectorError(AppModule)[SampleDirective -> ElementRef]: 
  StaticInjectorError(Platform: core)[SampleDirective -> ElementRef]: 
    NullInjectorError: No provider for ElementRef!

component:

template: `<div><h1 class="rotateBox" bsRotate>Test</h1></div>`,

directive:

@Directive({
  selector: '[bsRotate]'
})
export class SampleDirective {

  constructor(el: ElementRef) {
    console.log(el);
  }

}

Why i’m unable to use the element reference in my directive when generating a library?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:17

github_iconTop GitHub Comments

20reactions
swapnil-coditascommented, Feb 5, 2019

Same issue with angular 7

13reactions
rtmcommented, Jun 6, 2018

The paths option in tsconfig.json worked for me, after futzing around for hours.

"paths": {
  "@angular/*": ["../node_modules/@angular/*"]
}

Adding --preserve-symlinks to the build command, on the other hand, did not work for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullInjectorError: No provider for ElementRef - Stack Overflow
I got this NullInjector error when trying to use a UI control from a shared library module, adding the @angular path to my...
Read more >
NullInjectorError: No provider for ElementRef - EJ 2 Forums
to upload a document. But when I run the application with ng serve, I'm getting this error in the localhost. ERROR Error: StaticInjectorError( ......
Read more >
Angular – NullInjectorError: No provider for ElementRef
I created an internal directive in a module in my project (Angular 8 + Material Design). Following tuto and official doc. Ok, it's...
Read more >
Unhandled Promise rejection: No provider for ElementRef
There is something else wrong in your application. Some other hints: `bindings` is deprecated in favor of `providers`.
Read more >
Dependency injection in action - Angular
The first injector configured with a provider supplies the dependency (a service instance or value) to the constructor; If no provider is found...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found