AngularJS.页面重新加载后保持状态 [英] AngularJS. Retain state after page reload

查看:90
本文介绍了AngularJS.页面重新加载后保持状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用启用了html5Mode的UI Router,状态是从JSON加载的.

I am using UI Router with html5Mode enabled, states are loaded from JSON.

在F5之后或粘贴URL分别具有重新加载当前状态或导航到所述状态时的预期行为,而是加载了初始应用程序状态.

Expected behavior after F5 or when pasting URL is, respectively, having current state reloaded or navigating to the said state, instead the initial application state is loaded.

例如root/parent/child被重定向到root/.

For e.g. root/parent/child gets redirected to root/.

顺便说一句,使用ui-sref导航可以正常工作.

By the way, navigating with ui-sref works fine.

那么,页面重新加载后如何保留状态?

So, how can the state be retained after page reload?

推荐答案

似乎有点hacky,但目前可以使用.

Seems a bit hacky, but works for now.

app.run(['$location', '$state', function ($location, $state) {
  function stateFromUrl () {
    var path = $location.path(),
        hash = $location.hash();

    // do JSON states map parsing and find a corresponding to the URL state

    return state;
  }

  if (stateFromUrl) {
    $state.go(stateFromUrl);
  } else {
    $state.go('home'); // initial state
  }
}]);

这篇关于AngularJS.页面重新加载后保持状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆