generator-angular2-library: NullInjectorError: No provider for ElementRef!
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?
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 4
- Comments: 17
Same issue with angular 7
The
pathsoption intsconfig.jsonworked for me, after futzing around for hours.Adding
--preserve-symlinksto the build command, on the other hand, did not work for me.I had the same problem and I removed node_module folder and yarn.lock (or package-lock.json in case u are using npm) and installed dependencies again, now my problem is solved, hope it’s helpful
installing dependencies by:
I tired both the above solution, but no success. I am facing following error:
ERROR { Error: StaticInjectorError(AppServerModule)[NgClass -> ElementRef]: StaticInjectorError(Platform: core)[NgClass -> ElementRef]: NullInjectorError: No provider for ElementRef! at NullInjector.module.exports.NullInjector.get ngTempTokenPath: null, ngTokenPath: [ 'NgClass', [Function: ElementRef] ] }Also, i have upgraded my application from angular5 to angular 6 and trying to implement server-side rendering. Any solution?Thanks @AchiraFernando, it’s working!
Got it working by adding…
to defaults in .angular-cli.json file
I’m seeing the same issue with Angular 8:
I’ve tried the following:
node_modulesandpackage-lock.jsonand runningnpm installagain."preserveSymlinks": truetobuild.optionsin myangular.jsonfile.pathsproperty intsconfig.jsonwith"@angular/*": ["../node_modules/@angular/*"].… None of this worked. I can only get my attribute directive to work if I host it inside my project instead of attempting to load it as a third-party library.
I’ve even tried copying and pasting the attribute directive from the Angular docs as-is, without modifications, and the error still occurs.
I suspect this might be an issue with Angular itself, where attempting to load and use attribute directives from libraries simply doesn’t work.
Make the
pathsmapping in the application’s tsconfig (where you are linking to the library) and not the library one.I think
"preserveSymlinks": trueshould be inbuild.optionsIt’s relationed to install the library from local dir directly, when I publish the library to NPM and install from npm servers, works perfect