简体   繁体   English

AnguarJS中的鼠标单击/键盘按下事件

[英]Mouse click/keyboard press event in AnguarJS

function getNumberOfStores() {
    $http.get("api/Stores/getAll").then(function(response) {
        $scope.data = response.data;
        var time = new Date();
        window.localStorage.setItem("loadedTime", time);
  });
}

I have a function to call the API to get the number of stores and the last loaded time is stored in local storage.我有一个 function 调用 API 来获取存储数量,最后加载时间存储在本地存储中。 After detecting mouse/keyboard clicks anywhere on the website, if the current time - last loaded time is > 5 days, I need to refresh the page.检测到网站上任何地方的鼠标/键盘点击后,如果当前时间 - 上次加载时间> 5天,我需要刷新页面。 How can I check that the mouse is clicked or the keyboard is pressed in any of the controllers?如何检查在任何控制器中是否单击了鼠标或按下了键盘?

You could put ng-click and ng-keydown attributes on the body tag and then record those events however you like.您可以将 ng-click 和 ng-keydown 属性放在 body 标记上,然后根据需要记录这些事件。

<body ng-click=‘clickEvent()’ ng-keydown=‘typeEvent($event)’</body>

Then you could the do something with those events in your controller.然后你可以在你的 controller 中处理这些事件。

$scope.clickEvent = function(){
    // do work
};

$scope.typeEvent = function(event){
    // do work
};

Edit: a better approach would be to use the $window service and bind the click and keydown events.编辑:更好的方法是使用 $window 服务并绑定 click 和 keydown 事件。 Be sure to inject the $window service into your controller first.请务必先将 $window 服务注入您的 controller。

$window.addEventListener('click', function(){
    // record click event 
});

$window.addEventListener('keydown', function(){
    // record keydown event
});

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

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