How can i implement the same in angular ? I tried but Its giving error.
This is just plain JavaScript so it should work. Can you elaborate what error it is? Maybe it's something specific to angular and not related to this particular bit of code.
I am getting error like Property 'AmbientLightSensor' does not exist on type 'Window' & Cannot find name 'AmbientLightSensor'.
My Code: if (window.AmbientLightSensor) { const sensor = new AmbientLightSensor(); sensor.onreading = () => console.log(sensor.illuminance); sensor.onerror = event => console.log(event.error.name, event.error.message); sensor.start(); }
You need to extend the existing Window interface to tell it about this new property. So, before your class implementation add this code -
Window
class
declare global { interface Window { AmbientLightSensor: any; } }
Then you can add your AmbientLightSensor code in the following way -
AmbientLightSensor
if (window.AmbientLightSensor) { const sensor = new window.AmbientLightSensor(); sensor.onreading = () => console.log(sensor.illuminance); sensor.onerror = event => console.log(event.error.name, event.error.message); sensor.start(); }
I hope this will solve your problem.
Now error is not coming but window.AmbientLightSensor is coming as undefined.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
How can i implement the same in angular ?
I tried but Its giving error.
This is just plain JavaScript so it should work. Can you elaborate what error it is?
Maybe it's something specific to angular and not related to this particular bit of code.
I am getting error like Property 'AmbientLightSensor' does not exist on type 'Window' & Cannot find name 'AmbientLightSensor'.
My Code:
if (window.AmbientLightSensor) {
const sensor = new AmbientLightSensor();
sensor.onreading = () => console.log(sensor.illuminance);
sensor.onerror = event => console.log(event.error.name, event.error.message);
sensor.start();
}
You need to extend the existing
Window
interface to tell it about this new property.So, before your
class
implementation add this code -Then you can add your
AmbientLightSensor
code in the following way -I hope this will solve your problem.
Now error is not coming but window.AmbientLightSensor is coming as undefined.