简体   繁体   English

angularjs $ window.onclick的性能优于ng-click

[英]angularjs $window.onclick performs better than ng-click

I had a scenario where I have to make some alterations in localstorage as well as in some other data of the controller, this logic was supposed to be done on click of anywhere in whole page. 我有一个场景,我必须对本地存储以及控制器的其他数据进行一些更改,这种逻辑应该在整个页面上的任何位置单击即可完成。 This code was supposed to close all colorpickers if click was done anywhere on the body but not on the colorpicker button, but if clicked on colorpicker button then close all others colorpickers and open only the respective one. 如果单击是在身体的任何位置进行的,而不是在颜色选择器的按钮上进行的,则该代码应该关闭所有颜色选择器,但是如果单击颜色选择器的按钮,则关闭所有其他颜色选择器,并仅打开相应的颜色选择器。 First I tried using ng-click which was working okay but the pace of closing all colorpickers and opening of respective one was very slow. 首先,我尝试使用ng-click可以正常工作,但是关闭所有色标和打开各个色标的速度非常慢。

Note that I tried using $scope.$apply(); 请注意,我尝试使用$scope.$apply(); but that didn't work but instead it started giving me error that angularjs digestion is already in progress. 但这没有用,但是它开始给我一个错误,即我已在进行angularjs消化。 Then I used $window.onclick which worked perfectly and the pace of colorpickers closing and opening was also very fine. 然后,我使用了$window.onclick ,它工作得非常好,并且色标关闭和打开的速度也非常好。 I am wondering why $window.onclick worked better than ng-click . 我想知道为什么$window.onclickng-click更好。 That function is as follows 该功能如下

$window.onclick = function (e) {
        var labels = getAllLabels(vm.allData);
        if (!e.target.classList.contains('color-picker-button') || e.target.classList.contains('color-picker-popup') || e.target.classList.contains('buttons-popup-charts')) {
            for (var i = 0; i < labels.length; i++) {
                labels[i].isOpen = false;
                localStorageService.set(labels[i].name + '-isOpen-alpha', false);
            }
            init();
        }
        if (e.target.classList.contains('legends-charts')) {
            var id = e.target.id;
            for (var i = 0; i < labels.length; i++) {
                if (labels[i].name === id) {
                    labels[i].isOpen = !labels[i].isOpen;
                } else {
                    labels[i].isOpen = false;
                }
                localStorageService.set(labels[i].name + '-isOpen-alpha', labels[i].isOpen);
            }
            init();
        }
    };

$window is a reference to the browser's window object. $window是对浏览器window对象的引用。 (See here for details: https://docs.angularjs.org/api/ng/service/$window ) (有关详细信息,请参见此处: https : //docs.angularjs.org/api/ng/service/$window

ng-click causes a digest cycle through $scope.$apply . ng-click通过$scope.$apply引起摘要循环。 (Details here: https://docs.angularjs.org/guide/scope ) (详情请见: https : //docs.angularjs.org/guide/scope

Relevant part: 相关部分:

Listener directives, such as ng-click, register a listener with the DOM. 监听器指令(例如ng-click)会向DOM注册一个监听器。 When the DOM listener fires, the directive executes the associated expression and updates the view using the $apply() method. 当DOM侦听器触发时,伪指令执行关联的表达式并使用$ apply()方法更新视图。

Therefore, $window.onclick executes faster, because it doesn't cause a digest cycle. 因此, $window.onclick执行速度更快,因为它不会引起摘要循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM