2017-06-02 16 views
0

我是AngularJS的新手使用版本1.6.4,我想要做的是重新定向用戶在前一頁的完整刷新。現在我回來了,但頁面並不令人耳目一新。任何想法我怎麼能用一個單行代碼做到這一點。

用戶login.component.js:

(function() { 
    "use strict"; 

    var module = angular.module(__appName); 

    function controller(authService, $window, $location, $document) { 
     var model = this; 

     model.$onInit = function() { 
      //TODO: 
     }; 

     model.login = function() { 
      authService.login(model.email, model.password).then(function (response) { 
       //$window.history.back(); 
       //$window.history.go(-1); 
       //$window.location.href = '/'; 


       console.log("url:"+$document.referrer); 
       //$document.referrer is showing undefined in console 
       $location.replace($document.referrer); 

      }, 
      function (response) { 
       model.msg = response.error; 
      }); 
     } 
    } 

    module.component("userLogin", { 
     templateUrl: "components/user-login/user-login.template.html", 
     bindings: { 
      email: "<", 
      password: "<" 
     }, 
     controllerAs: "model", 
     controller: ["authService", "$window", "$location", "$document" controller] 
    }); 
}()); 

App.js:

"use strict"; 

//Global variables 
var __apiRoot = "http://localhost:8000/api"; //No slash '/' at the end 


var module = angular.module(__appName, [ 
    "ui.router", 
    "angular-jwt" 
]); 

module.config(function ($stateProvider, $urlRouterProvider, $httpProvider, jwtOptionsProvider) { 

    $urlRouterProvider.otherwise('/app/home'); 

    $stateProvider 

     .state("app", { 
      abstract: true, 
      url: "/app", 
      component: "appRouting" 
     }) 

     .state("app.home", { 
      url: "/home", 
      component: "homeRouting" 
     }) 

     .state("app.search", { 
      url: "/search/:q", 
      component: "searchRouting" 
     }); 

    jwtOptionsProvider.config({ 
     tokenGetter: ['authService', function (authService) { 
      return authService.getToken(); 
     }], 
     whiteListedDomains: ['localhost'] 
    }); 

    $httpProvider.interceptors.push('jwtInterceptor'); 
}); 

回答

1

如果您正在使用角UI路由器,並有以前的狀態的名稱,然後使用:

$state.go('previousState', {}, { reload: true }); 

如果沒有以前的狀態的名字,那麼你可以使用這段代碼它會在每次狀態改變都會發生時運行。

$rootScope.previousState; 
$rootScope.currentState; 
$rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from,fromParams) { 
    $rootScope.previousState = from.name; 
    $rootScope.currentState = to.name; 
    console.log('Previous state:'+$rootScope.previousState) 
    console.log('Current state:'+$rootScope.currentState) 
}); 
+0

你能告訴我如何添加這段代碼,因爲我是一個新手而且很煩惱要這樣做,因爲出錯了。 – MTA

+0

我在文章中也添加了我的文件的app.js代碼。 – MTA

0

,你可以簡單地使用原生的JavaScript刷新頁面。 window.location.reload()

+0

它只會重新加載當前頁面。我想要的是重新加載上一頁。 – MTA

+0

首先將其重定向到上一頁,然後使用'location.replace()'重新加載 –

+1

。您可以使用document.referrer獲取之前的URL。 'location.replace(document.referrer)'; – Kasiriveni

0

使用$location.replace()。您可以使用$ document.referrer獲取之前的URL。 $location.replace($document.referrer)

+0

$ document.referrer在控制檯中顯示爲undefined。而我的代碼卡在那裏,因爲我沒有重定向到任何地方 – MTA

+0

我更新了帖子,告訴你我怎麼做$ location.replace – MTA