Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度指令中的窗口级事件未按预期执行 标题可能令人困惑。请查找以下代码: //html //模块 var myApp=angular.module('app',[]); myApp.directive('test',function(){ 返回{ 链接:功能(范围、el、属性){ window.onbeforeunload=函数(){ console.log(“test1”); } } }; })_Angularjs_Javascript Events - Fatal编程技术网

Angularjs 角度指令中的窗口级事件未按预期执行 标题可能令人困惑。请查找以下代码: //html //模块 var myApp=angular.module('app',[]); myApp.directive('test',function(){ 返回{ 链接:功能(范围、el、属性){ window.onbeforeunload=函数(){ console.log(“test1”); } } }; })

Angularjs 角度指令中的窗口级事件未按预期执行 标题可能令人困惑。请查找以下代码: //html //模块 var myApp=angular.module('app',[]); myApp.directive('test',function(){ 返回{ 链接:功能(范围、el、属性){ window.onbeforeunload=函数(){ console.log(“test1”); } } }; }),angularjs,javascript-events,Angularjs,Javascript Events,我已经定义了“window.onbeforeunload”事件回调函数。我对两个不同的dom元素有相同的指令。当引发“onbeforeunload”事件时,它只执行一次。我认为它会执行两次,以便捕获元素级别的信息。但这并没有发生。它仅在元素“test1”的上下文中执行。可能是我做错了什么。请输入..窗口。onbeforeunload只能接受一个事件处理程序函数,因此每次分配一个新函数时,都会删除现有的函数。解决这个问题的一种方法是,如果已经定义了一个函数,则将其“附加”到函数中(也更改为使用$w

我已经定义了“window.onbeforeunload”事件回调函数。我对两个不同的dom元素有相同的指令。当引发“onbeforeunload”事件时,它只执行一次。我认为它会执行两次,以便捕获元素级别的信息。但这并没有发生。它仅在元素“test1”的上下文中执行。可能是我做错了什么。请输入..

窗口。onbeforeunload
只能接受一个事件处理程序函数,因此每次分配一个新函数时,都会删除现有的函数。解决这个问题的一种方法是,如果已经定义了一个函数,则将其“附加”到函数中(也更改为使用
$window
服务以提高可测试性)

Title may be confusing. Please find 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");

                }
            }
        };
    })
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;
                }
            }

        }
    };
});