Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly add document.addEventListener to Angular2?

Tags:

angular

I've came across cordova RemoteControl plugin where, to listen to its event, the addEventListener is needed to be registered.

//listen for the event
document.addEventListener("remote-event", function(event) {
//do something
});

Is it a proper way for me to do it?

like image 752
Vicheanak Avatar asked Dec 06 '22 16:12

Vicheanak


2 Answers

Try the below step to add event listener using angular 2. I am not sure about "remote-event" event but "click" event is working fine for me as given below.

import { ElementRef, Renderer } from '@angular/core';

constructor(public el: ElementRef, public renderer: Renderer) { 
   renderer.listenGlobal('document', 'click', (event) => {
     // Do something with 'event'
   });
}

Update

As per comment below by @paqogomez.

Renderer has been renamed to Renderer2 and listenGlobal to listen.

like image 190
Dipak Delvadiya Avatar answered Dec 09 '22 14:12

Dipak Delvadiya


this works for me like magic

 document.querySelector('#videostop').addEventListener('click', () => {
            //function here 
            console.log('your result');
         });
like image 23
Kofi Sammie Avatar answered Dec 09 '22 15:12

Kofi Sammie