在语言js中只重新加载一次页面



我使用的是angularjs,我希望在加载页面时只刷新一次,我尝试的是:

<script>
app.cp.register('userProfileController', function ($window) {
    debugger;
    function reload() {
        $window.location.reload();
    }
    reload();
});

但是页面会以这种方式无限次刷新,这是怎么回事呢?

当刷新页面时,所有内容都被擦除,并且您正在重新注册控制器并执行reload(),所以是的,它将继续发生。

为了在页面刷新之间保持数据,您可以使用ngStorage:

app.cp.register('userProfileController', function ($window, $localStorage) {
  debugger;
  function reload() {
    $localStorage.hasReloaded = true;
    $window.location.reload();
  }
  if (!$localStorage.hasReloaded) reload();
});

最新更新