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

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

问题描述

我正在使用启用了 html5Mode 的 UI 路由器,状态是从 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天全站免登陆