0

I hope my question as not asking before.

I'm working with Angular9 and a JS plugin (A-Frame).

I prepared a script is get data and I want to return this data in my component angular for managing his after.

AFRAME.registerComponent('cursor-listener', {
  init: function () {
      this.el.addEventListener('click', function (evt) {
          console.log('I was clicked at: ', evt.detail.intersection.object);
      });
  }
});

In the script you see above, the console log returns information that is useful to me. I would like to be able to return them in my angular component to process the information later.

I'm beginner with this framework, so excuse me for my ignorance.

Thank you for your futur response, I waiting to read you.

Artiøs
  • 3
  • 1

1 Answers1

0

You could use arrow function notation to refer to the class member variables using this keyword. In a conventional JS function, this keyword refers to the scope of the function.

intersectionObj: any;

AFRAME.registerComponent('cursor-listener', {
  init: () => {         // <-- arrow function here
    this.el.addEventListener('click', (evt) => {       // <-- and here
      console.log('I was clicked at: ', evt.detail.intersection.object);
      this.intersectionObj = evt.detail.intersection.object;
    });
  }
});
ruth
  • 29,535
  • 4
  • 30
  • 57
  • Thank you for your quick response but my code presented in my post is javascript code. And arrow doesn't work like "intersectionObj: any;" – Artiøs Jun 04 '20 at 07:57
  • But I see `this.el` which possibly refers to Angular `ElementRef`? It leads me to believe this code is inside an Angular component. – ruth Jun 04 '20 at 07:58
  • No, the script is not internal to the component because the A-Frame plugin that I use needs a js manipulation in an external file so my question is how to exchange information between this famous external script and my component. – Artiøs Jun 04 '20 at 08:27
  • You could refer here to attach external Javascript files to Angular: https://stackoverflow.com/q/41120754/6513921 – ruth Jun 04 '20 at 08:55
  • I think your close to my problem, but in this subject I don't understand were I could put my line "declare var xxxxx: any; " ? – Artiøs Jun 04 '20 at 09:27
  • You could put it at the top of the file (outside the class) where you wish to access the library. – ruth Jun 04 '20 at 09:28