How To Manage F5 Event In AngularJS

Step 1

First of all, we will add a directive with the name shortcuts in a div tag, where all your HTML web page contents are placed .

In the .HTML page, execute the code given below.

  1. <div shortcuts=”keyUp”> //Here in this div tag your html page content on which you want to perform F5 action   
  2.   </div>   

Step 2

Add the Directive given below in your controller.js with Directive name shortcuts, which comes under your specific module.

Controller.js

Add this Directive=>shortcuts. 

  1. angular.module(“SandipPatil’ sModule”).directive(‘shortcuts‘, [‘$document’, ‘$rootScope’, function($document, $rootScope) {  
  2.                 $rootScope.shortcuts = [];  
  3.                 $document.on(‘keydown keyup‘, function(e) {  
  4.                     // Skip if it focused in input tag.  
  5.                     if (event.target.tagName !== “INPUT”) {  
  6.                         $rootScope.shortcuts.forEach(function(eventHandler) {  
  7.                             // Skip if it focused in input tag.  
  8.                             if (event.target.tagName !== ‘INPUT’ && eventHandler) eventHandler(e.originalEvent, e)  
  9.                         });  
  10.                     }  
  11.                 })  
  12.                 return {  
  13.                     restrict: ‘A’,  
  14.                     scope: {‘  
  15.                         shortcuts’: ‘ & ’  
  16.                     },  
  17.                     link: function(scope, element, attrs) {  
  18.                         $rootScope.shortcuts.push(scope.shortcuts());  
  19.                     }  
  20.                 };   

Step 3

Now, whenever the user presses F5 key, it will fire Angular function given below. Thus, write the code given below for this purpose.

Use it in the format given below in any controller.js.

  1. $scope.keyUp = function (key) {  
  2. //116 is for F5  
  3. if (key.keyCode==116)  
  4. $state.go(“common.login”);  

Or

  1. window.location.href=”/login”;  
  2.   
  3. //e.g.Here user is navigated to login screen  
  4. };   

Summary

When the user comes on the particular page and hits F5, the user will be redirected to login page.