How to call an Angular 4 method from a standalone plain JavaScript function?

后端 未结 4 1332
执念已碎
执念已碎 2021-01-03 09:47

I\'d like to be able to pass some data\\propagate events from a plugin on the page to my Angular 4 app.

More specifically, in my case data\\events are generated insi

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-03 10:06

    You can use a events, so you have the JavaScript send an event that you are listening for in your service.

    The CustomEvent is a new feature so you might need to polyfill it: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

    JavaScript:

    var event = new CustomEvent("CallAngularService");
    
    window.dispatchEvent(event);
    

    Angular:

    @HostListener("window:CallAngularService")
    onCallAngularService() {
      // Run your service call here
    }
    

提交回复
热议问题