jvandemo / jvandemo/generator-angular2-library

NullInjectorError: No provider for ElementRef!

Open
#281 17 comments 4 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
740
Forks
116
PR merge metrics
No merged PRs in 30d

Description

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: `

Test

`,```

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?

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue identifies the generated Angular library's SampleDirective and its consumer template; start by reproducing the constructor injection in that setup and inspecting how the library is built and consumed. Done means the directive can inject ElementRef when applied to the shown element without a NullInjectorError.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.