简体   繁体   English

如何使用AngularJS刷新(F5)并获取另一个页面?

[英]How to Refresh (F5) and get another page with AngularJS?

In my web application, I got a form on 2 different pages, purchase1 and purchase2 . 在我的Web应用程序中,我在2个不同的页面上分别有一个表单purchase1purchase2

If a customer refreshes the page at purchase2 , I want the location to be changed back to purchase1 . 如果客户在purchase2刷新页面,我希望将位置更改回purchase1

I haven't found a way to do so, I've tried to make a config like that: 我还没有找到一种方法,我试图做这样的配置:

.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.when('/purchase2', '/purchase1');
}

But obviously, that way I can never get to purchase2 page. 但是显然,这样一来,我将永远无法进入purchase2页面。

I need it to happen only on manual user Refresh. 我需要仅在手动用户刷新时发生。

Is there any way to do so? 有什么办法吗? Some built-in Angular function that happens on Refresh? 刷新时会发生一些内置的Angular函数吗?

Something like 就像是

$scope.onRefresh = function(){
   $location.path('/dashboard/purchase1');
}

Haven't found anything like it. 找不到类似的东西。

You can listen for beforeunload event. 您可以监听beforeunload事件。 beforeunload event will be triggered when someone hits a F5 or refreshes the page anyhow. 当有人按下 F5或刷新页面时,将触发beforeunload事件。 Do something like, 做类似的事情,

var windowElement = angular.element($window);
windowElement.on('beforeunload', function (event) {
    event.preventDefault();
    //below is the redirect part.
    $window.location.href = '/purchase1';
});

Put this code on purchase2 page. 将此代码放在Purchase2页面上。

Yes, you can do it. 是的,您可以做到。

Register a global listener for state change: 注册全局侦听器以进行状态更改:

$scope.$on('$stateChangeStart', function(event, toState, toParams, fromState) {
    // If fromState is null that means it is a page refresh otherwise
    // The fromState will be the purchase1
    if (toState.name == 'purchase2' && !fromState) {
        $state.go('purchase1');
        event.preventDefault();
        return;
    }
});

By global, I mean register the above in such a controller whose scope is available throughout your application like a controller added to your body or html tag. 就全局而言,我的意思是将上述内容注册到这样一个控制器中,该控制器的作用域在整个应用程序中都可用,例如添加到您的bodyhtml标签中的控制器。

Also, I'm assuming the purchase1 & purchase2 are the name of your states for both the pages. 另外,我假设purchase1purchase2是两个页面的州名。 If they are parameters to a common state you can alter the code above. 如果它们是通用状态的参数,则可以更改上面的代码。

What happens on refresh is the same as what happens on first load. 刷新时发生的情况与第一次加载时发生的情况相同。

What you can do is check whether the user has been here already, eg by setting a cookie and reading it later. 您可以做的就是检查用户是否已经来过这里,例如设置cookie并稍后阅读。

However, I think you should rethink your design. 但是,我认为您应该重新考虑您的设计。 Why would you invest your time into making refresh function behave worse? 您为什么要花时间使刷新功能表现更糟?

I think, javascript isnt the right way because javascript does not know anything about the previous page, because it will be rendered when page is already loaded. 我认为,javascript不是正确的方法,因为javascript对上一页不了解,因为它将在页面已加载时呈现。 So try to check this in your server side application like in php or jsp and read the request-header because there you can get current url and redirect user to the new url 因此,请尝试像在php或jsp中那样在服务器端应用程序中进行检查,并阅读请求标头,因为在那里您可以获得当前url并将用户重定向到新的url。

You can try to have a flag say purchase2-loaded and keep this variable in localStorage . 您可以尝试加载一个标志,例如purchase2-loaded ,并将此变量保留在localStorage You can then write an IIFE, which will check value of this variable and reroute to purchase1 . 然后,您可以编写一个IIFE,这将检查该变量的值,并重新路由到purchase1。

Also on purchase1 , reset it to false . 同样在Purchase1中 ,将其重置为false

Note: This is a pure JS Code and relies on localStorage. 注意: 这是纯JS代码,并依赖于localStorage。

Fiddle . 小提琴

Sample Code 样例代码

 (function(){ var isPurchase2Loaded = localStorage.getItem("Purchase2"); if(isPurchase2Loaded || isPurchase2Loaded === undefined){ document.write("Rerouting to purchase 1..."); } else{ document.write("Loading for the first Time..."); localStorage.setItem("Purchase2", true); } })() 

Eventually, if someone is interested, I fixed it like that: 最终,如果有人感兴趣,我将其修复为:

In purchase1.js I've added this to the submit() function: 在purchase1.js中,我已将其添加到submit()函数中:

$rootscope.demographic=1;

In purchase2.js I've added this code to the controller: 在purchase2.js中,我将此代码添加到了控制器中:

var init = function(){
      if(angular.isUndefined($rootScope.demographic)){
           $location.path('/purchase1');    
      }

    };
    init();

It is working because Refresh(F5) completely restarting the application, therefore also resets the $rootscope and makes "Demographic" Undefined. 之所以起作用,是因为Refresh(F5)完全重新启动了应用程序,因此也重置了$ rootscope并使“ Demographic”处于未定义状态。

Upon entering purchase2, "init" function will start, if we came from purchase1 everything will be ok and demographic will be defined, otherwise we will just load purchase1 again. 进入purchase2后,将启动“ init”功能,如果我们来自purchase1,一切正常,将定义人口统计信息,否则我们将再次加载purchase1。

:) :)

$state.go('purchase1'); 

这应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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