javascript - Call a function before refreshing the page -
when user hit f5 or refresh button need call function before refreshing page, tried below code , did'nt work, please suggest me i'm doing wrong , there better way this. i'm using anuglar 1.5 ui-router.
angular.module('module.name', [ ]).controller('somecontroller',['$scope', function($scope) { $scope.$on("$statechangestart", function () { functiontocallonpagerefresh(); }); }]);
functiontocallonpagerefresh() not getting called on page refresh.
you need create parent state 'secure' , inherit every state in application state as-
angular.module('modulename').config(['$stateprovider', '$urlrouterprovider', function ($stateprovider, $urlrouterprovider) { $stateprovider. state('secure', { url: "/", abstract: true, templateurl: "/path/to your/ master page", resolve: { factory: 'checkrouting' } }). state('dashboard', { url: 'dashboard', templateurl: 'path/to /template', parent: 'secure', }); } ]);
here have mentioned
resolve: { factory: 'checkrouting' }
in checkrouting
inside resolve property, factory going task (check user login or not) on sate change or press f5.
in 'secure' state use 'resolve' property execute function if user press f5 or state change as-
(function() { 'use strict'; angular.module('modulename').factory('checkrouting', ['$rootscope','$timeout' checkrouting]); function checkrouting($rootscope,$timeout) { if ( condition) { //user loged in return true; } else { $timeout(function() { $state.go('login'); }); return false; } } }());
Comments
Post a Comment