Windows.history.back() + location.reload() jquery

前端 未结 7 1267
[愿得一人]
[愿得一人] 2021-02-05 05:30

I\'ve a probleme in my code. The aim is to complete a simple form, then you click on a submit button. It do an Ajax resquest to go in the method. On success in the ajax request,

7条回答
  •  自闭症患者
    2021-02-05 06:17

    window.history.back() does not support reload or refresh of the page. But you can use following if you are okay with an extra refresh

    window.history.back()
    window.location.reload()
    

    However a real complete solution would be as follows: I wrote a service to keep track of previous page and then navigate to that page with reload:true

    Here is how i did it.

    'use strict';
    
    angular.module('tryme5App')
        .factory('RouterTracker', function RouterTracker($rootScope) {
              var routeHistory = [];
              var service = {
                getRouteHistory: getRouteHistory
              };
    
              $rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
                  routeHistory = [];
                  routeHistory.push({route: from, routeParams: fromParams});
              });
    
              function getRouteHistory() {
                return routeHistory;
              }
    
              return service;       
        });
    

    Make sure you have included this js file from you index.html

    
    

    Now from you stateprovider or controller you can access this service and navigate

    var routeHistory = RouterTracker.getRouteHistory();    
    console.log(routeHistory[0].route.name)
    $state.go(routeHistory[0].route.name, null, { reload: true });
    

    or alternatively even perform checks and conditional routing

    var routeHistory = RouterTracker.getRouteHistory();    
    console.log(routeHistory[0].route.name)
    if(routeHistory[0].route.name == 'seat') {
          $state.go('seat', null, { reload: true });
    } else {
          window.history.back()
    }
    

    Make sure you have added RouterTracker as an argument in your function in my case it was :

    .state('seat.new', {
                    parent: 'seat',
                    url: '/new',
                    data: {
                        authorities: ['ROLE_USER'],
                    },
                    onEnter: ['$stateParams', '$state', '$uibModal', 'RouterTracker', function($stateParams, $state, $uibModal, RouterTracker) {
      $uibModal.open({
          //....Open dialog.....
     }).result.then(function(result) {
                var routeHistory = RouterTracker.getRouteHistory();    
                console.log(routeHistory[0].route.name)
                $state.go(routeHistory[0].route.name, null, { reload: true });
     }, function() {
                        $state.go('^');
     })
    

提交回复
热议问题