This code snippet might be a bit confusing. Take a look at the code below:
//html
<div id="test1" test></div>
<div id="test2" test></div>
//module
var myApp = angular.module('app', []);
myApp.directive('test', function () {
return {
link: function (scope,el,attr) {
window.onbeforeunload = function () {
console.log("test1");
}
}
};
})
I am using a window.onbeforeunload
event callback function in this code. The directive 'test' is applied to two different DOM elements. When the onbeforeunload
event occurs, it only executes once. I expected it to execute twice so that I could gather information about each element individually. However, it seems to only run in the context of the 'test1' element. If there are any suggestions or insights on what might be causing this behavior, please let me know.