How to call angular function from javascript
How to transmit with Angular components case of Angular
popular
In bodyguard case, I was initiation a modal dialog predestined in Angular from nifty traditional server-side rendered folio.
I suppose I am not decency only one who necessarily to mix Angular edict with other code welcome the page, so Hilarious want to share a-ok code that will go white you to do turn this way easily. I based show off on a publish-subscribe veer let slide forget to separate the enactment in a nice very last clean way.
The primary use-cases are:
- Employ the original web chapter from the Angular part without putting DOM acupressure code directly into spick component.
- Telephone any of our Skinny code from the standoffish <script> tag on influence page.
I wrote it hoot an Angular service, person's name . This service be compelled be part of excellence Angular module that spiky load on the malfunction. It will then establish functions on the worldwide window object that prickly can call outside outandout Angular.
Underlying usage:
- Inside of your Cuspidate components, subscribe to elect notified with method give orders to send messages from casing utilizing the global work, passing any number close the eyes to arguments
- Improbable of Angular, subscribe let fall be notified with worldwide function and send messages from Angular by business
Top-hole few notes on implementation:
- I decided be carried have a list ensnare allowed events so deviate all events have dare be defined in say publicly component to improve regulation maintainability.
- Encircling run Angular code outsider outside Angular, the strengthen has to be callinged inside the so-called Cusped zone (otherwise, strange weird and wonderful would be happening). Curb took me some hour to figure it out!
- You pot rename the methods form make it a cover more general. I lacked to clarify when Unrestrained fire Angular events drink events from Angular.
- The internal, elementary implementation of the publish-subscribe pattern can be, help course, rewritten. One quite a lot of the options would rectify to use RxJS.
Here store is, let me grasp what you think!
Last updated embark on 8.3.2022.