简体   繁体   中英

How to stop page refresh on click of back button in angularjs

HTML:

<button type="button" class="btn btn-success btn-sm" ng-click="goBack($event);"> 
   Back
</button>

Model:

$scope.goBack = function ($event) {
   window.history.back();
   $event.preventDefault();
   $event.stopPropagation();
}

ui-router doesn't track the previous state once it transitions, but the event $stateChangeSuccess is broadcast on the $rootScope when the state changes.

You should be able to catch the prior state from that event (from is the state you're leaving)

$rootScope.$on('$stateChangeSuccess', function (event, toState,
   toParams, from, fromParams) {
       $rootScope.previousState = from.name;
       $rootScope.currentState = to.name;
}); 

Angularjs applications are single page applications so back should not refresh page. Although I suppose that the urls present have "#" present in them , so window.location.onhashchange event you can override.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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