3

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

This is the error I am getting in angular5 when I am making use of ElementRef in one of my services and I did import it like this:

import { Injectable,ElementRef } from '@angular/core';
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Avinash.k
  • 33
  • 1
  • 1
  • 3

1 Answers1

12

You can't inject ElementRef to a service class,

it is used only on component or directive,

maintain service class to write business logic only,

and try to use ElementRef in your component

or create a directive and try to use that directive.

For more details you can refer the below answer link,

Injecting ElementRef to injecable error

JpG
  • 774
  • 8
  • 22
  • you should be using a component anyway :) think of the component as a parent that can keep track of different states (and therefore will know when and how to interact with services according to how you tell it). it allows you to leave the business logic to services so they don't also keep track of what the component already is. – Yoko Ishioka Dec 13 '19 at 05:18