How to properly attach an Event Listener to global 'window' in Angular 6+? [duplicate]












0
















This question already has an answer here:




  • Angular window resize event

    12 answers




Background



Our Angular 6 application contains an iframe with some 3rd party data. To enable a safe cross-domain communication the child iframe posts messages to parent Angular application using window.postMessage() API. In order to receive those messages parent window must attach an event listener like window.addEventListener("message", callback) to listen for MessageEvents



Question



I need to listen for message events inside Angular component and it's not obvious for me how to properly access global window in Angular way. The only solution I imagine at the moment is:




  1. Create an Angular Service called MessageListenerService with the following method:


function listenForMessageEvent(callback) {
this.renderer.listen('window', 'message', callback);
}



Note: this.renderer here is Renderer2 from Angular core.




  1. Just use MessageListenerService.listenForMessageEvent() in my custom component.


I feel like it's not the standard approach in Angular world. Could someone advise any best practise here?



Thanks a lot.










share|improve this question













marked as duplicate by ConnorsFan, Community Nov 26 '18 at 7:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Angular window resize event

      12 answers




    Background



    Our Angular 6 application contains an iframe with some 3rd party data. To enable a safe cross-domain communication the child iframe posts messages to parent Angular application using window.postMessage() API. In order to receive those messages parent window must attach an event listener like window.addEventListener("message", callback) to listen for MessageEvents



    Question



    I need to listen for message events inside Angular component and it's not obvious for me how to properly access global window in Angular way. The only solution I imagine at the moment is:




    1. Create an Angular Service called MessageListenerService with the following method:


    function listenForMessageEvent(callback) {
    this.renderer.listen('window', 'message', callback);
    }



    Note: this.renderer here is Renderer2 from Angular core.




    1. Just use MessageListenerService.listenForMessageEvent() in my custom component.


    I feel like it's not the standard approach in Angular world. Could someone advise any best practise here?



    Thanks a lot.










    share|improve this question













    marked as duplicate by ConnorsFan, Community Nov 26 '18 at 7:43


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Angular window resize event

        12 answers




      Background



      Our Angular 6 application contains an iframe with some 3rd party data. To enable a safe cross-domain communication the child iframe posts messages to parent Angular application using window.postMessage() API. In order to receive those messages parent window must attach an event listener like window.addEventListener("message", callback) to listen for MessageEvents



      Question



      I need to listen for message events inside Angular component and it's not obvious for me how to properly access global window in Angular way. The only solution I imagine at the moment is:




      1. Create an Angular Service called MessageListenerService with the following method:


      function listenForMessageEvent(callback) {
      this.renderer.listen('window', 'message', callback);
      }



      Note: this.renderer here is Renderer2 from Angular core.




      1. Just use MessageListenerService.listenForMessageEvent() in my custom component.


      I feel like it's not the standard approach in Angular world. Could someone advise any best practise here?



      Thanks a lot.










      share|improve this question















      This question already has an answer here:




      • Angular window resize event

        12 answers




      Background



      Our Angular 6 application contains an iframe with some 3rd party data. To enable a safe cross-domain communication the child iframe posts messages to parent Angular application using window.postMessage() API. In order to receive those messages parent window must attach an event listener like window.addEventListener("message", callback) to listen for MessageEvents



      Question



      I need to listen for message events inside Angular component and it's not obvious for me how to properly access global window in Angular way. The only solution I imagine at the moment is:




      1. Create an Angular Service called MessageListenerService with the following method:


      function listenForMessageEvent(callback) {
      this.renderer.listen('window', 'message', callback);
      }



      Note: this.renderer here is Renderer2 from Angular core.




      1. Just use MessageListenerService.listenForMessageEvent() in my custom component.


      I feel like it's not the standard approach in Angular world. Could someone advise any best practise here?



      Thanks a lot.





      This question already has an answer here:




      • Angular window resize event

        12 answers








      javascript angular dom iframe postmessage






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 15:41









      AlexanderAlexander

      51




      51




      marked as duplicate by ConnorsFan, Community Nov 26 '18 at 7:43


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by ConnorsFan, Community Nov 26 '18 at 7:43


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          The "Angular" way of doing this is to use HostListener



          https://angular.io/api/core/HostListener



          Here is a stackblitz example for your review. Open console and review the log when using keys on the keyboard to trigger events.



          https://stackblitz.com/edit/hostlistener-1






          share|improve this answer


























          • Thank you, looks like a correct solution

            – Alexander
            Nov 26 '18 at 7:44











          • No problem, please be sure to mark it as an accepted answer.

            – Marshal
            Nov 26 '18 at 13:08




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The "Angular" way of doing this is to use HostListener



          https://angular.io/api/core/HostListener



          Here is a stackblitz example for your review. Open console and review the log when using keys on the keyboard to trigger events.



          https://stackblitz.com/edit/hostlistener-1






          share|improve this answer


























          • Thank you, looks like a correct solution

            – Alexander
            Nov 26 '18 at 7:44











          • No problem, please be sure to mark it as an accepted answer.

            – Marshal
            Nov 26 '18 at 13:08


















          0














          The "Angular" way of doing this is to use HostListener



          https://angular.io/api/core/HostListener



          Here is a stackblitz example for your review. Open console and review the log when using keys on the keyboard to trigger events.



          https://stackblitz.com/edit/hostlistener-1






          share|improve this answer


























          • Thank you, looks like a correct solution

            – Alexander
            Nov 26 '18 at 7:44











          • No problem, please be sure to mark it as an accepted answer.

            – Marshal
            Nov 26 '18 at 13:08
















          0












          0








          0







          The "Angular" way of doing this is to use HostListener



          https://angular.io/api/core/HostListener



          Here is a stackblitz example for your review. Open console and review the log when using keys on the keyboard to trigger events.



          https://stackblitz.com/edit/hostlistener-1






          share|improve this answer















          The "Angular" way of doing this is to use HostListener



          https://angular.io/api/core/HostListener



          Here is a stackblitz example for your review. Open console and review the log when using keys on the keyboard to trigger events.



          https://stackblitz.com/edit/hostlistener-1







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 13:09

























          answered Nov 23 '18 at 16:29









          MarshalMarshal

          2,8022418




          2,8022418













          • Thank you, looks like a correct solution

            – Alexander
            Nov 26 '18 at 7:44











          • No problem, please be sure to mark it as an accepted answer.

            – Marshal
            Nov 26 '18 at 13:08





















          • Thank you, looks like a correct solution

            – Alexander
            Nov 26 '18 at 7:44











          • No problem, please be sure to mark it as an accepted answer.

            – Marshal
            Nov 26 '18 at 13:08



















          Thank you, looks like a correct solution

          – Alexander
          Nov 26 '18 at 7:44





          Thank you, looks like a correct solution

          – Alexander
          Nov 26 '18 at 7:44













          No problem, please be sure to mark it as an accepted answer.

          – Marshal
          Nov 26 '18 at 13:08







          No problem, please be sure to mark it as an accepted answer.

          – Marshal
          Nov 26 '18 at 13:08





          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python