I'm facing an issue with Zone.js and non-angular DOM element events.
This happens when we assign a javascript function to any DOM element in the HTML mark up and then programmatically re-assign the same DOM element some other or the same function.
e.g. You have this mark up
<button id="button1" onclick="test1()">button 1</button>
and then somewhere in JS you re-assign button1 onclick with some other function
document.getElementById('button1').onclick = test3;
In such scenarios, two function calls are made. First one is original assigned method and then the second one is the newly assigned method.
I’ve created a PLUNKER to demo this behavior.
In the example, we have an angular app and 2 non angular buttons on the screen. Each button is initially assigned a function which just prints some text to console.
One button (Button 1 on the onclick event ) just prints test 1 method called.
The other button (Button 2 on the onclick event) prints test 2 method called. And also assigns onclick of button1 a new function. This new function prints test 3 method called.
The scenario-
Let the application load. When you can see App works do the following:-
1. Open the debugger console.
2. Click button 2
3. Check the console
4. Click button 1.
5. Check the console.
The expectation.
• On click on Button 2 , we should see test 2 method called.
• On click on Button 1 , we should see test 3 method called.
The actual
• On click on Button 2, we see test 2 method called.
• On click on Button 1, we see test 1 method called. Followed by test 3
method called.
The PLUNKER link
https://plnkr.co/edit/ymKcHjWPrDiG73NfWBle?p=preview
On debugging , I see that one method is called DOM element event (test1 from onclick) and the other method (test3) is called by zonejs
(To this debugging, I had added console.log in zone.js file on my local machine)
I see that zonejs registers with allmost all events related to any DOM element on the screen.
So, is this an issue I'm facing ? or is it the expected behavior or am I going wrong some where? Please help with some info / pointers.