angularjs - Window level events in angular directives are not executing as expected -


title may confusing. please find 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 have " window.onbeforeunload" event callback function defined. have same directive on 2 different dom elements.when "onbeforeunload" event raised, executed once. thought execute twice can capture element level info. not happening. executing in context of element "test1". may doing wrong.any inputs please..

window.onbeforeunload can take 1 event handler function, each time new 1 assigned, existing 1 removed. 1 way around "append" function if there's 1 defined (also changed use $window service better testability)...

myapp.directive('test', function ($window) {      return {          link: function (scope, el, attr) {             appendonbeforeunload(function () {                 console.log(el.attr('id'));             });              function appendonbeforeunload(fn) {                 var currentfn = $window.onbeforeunload;                 if (angular.isfunction(currentfn)) {                     $window.onbeforeunload = function () {                         currentfn();                         fn();                     };                 } else {                     $window.onbeforeunload = fn;                 }             }          }     }; }); 

jsfiddle


Comments

Popular posts from this blog

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

php - facebook and github login HWIOAuthBundle and FOSUserBundle in Symfony2.1 -

hadoop - Sqoop installation export and import from postgresql -