简体   繁体   English

页面重新加载后菜单的保存状态

[英]Saving state of menu after page reload

I have treeview menu where I would like to remember its state after page reloads. 我有treeview菜单,在这里我想记住页面重新加载后的状态。 To reload page I'm using angularJS function $state.reload(); 要重新加载页面,我正在使用angularJS函数$ state.reload();。

I don't know should I use Jquery to collect cookies about current state of menu and save it, or maybe there is a way to collect data about current state using angular. 我不知道我应该使用Jquery来收集有关菜单当前状态的cookie并将其保存,还是有一种方法可以使用angular来收集有关当前状态的数据。

Any ideas and suggestions would be highly appreciated. 任何想法和建议将不胜感激。

Here is my HTML 这是我的HTML

<nav class="side-nav" id="menu">
<ul>
    <li class="side-nav-item">
        <a href="/index">
            <i class="nav-item-icon icon ion-ios7-navigate-outline"></i>
            Main page
        </a>
    </li>
    <li class="side-nav-item collapsable">
        <a href="javascript:void(0);">
            <i class="nav-item-icon icon icon ion-chevron-right"></i>
            1. subitem
        </a>
        <ul style="display:none;">
            <li class="side-nav-item">
                <a ui-sref=".countiesandtowns">
                    <i class="nav-item-icon icon ion-clipboard"></i>
                    2. subitem
                </a>
            </li>
        </ul>
    </li>
</ul>

And here is JQuery I'm using 这是我正在使用的jQuery

$(function () {
$(document).ready(function () {
    $("#menu").treeview({
        toggle: function() {
            var element = $(this).find(">a>i");

            if (element) {
                var rightElement = element.hasClass("ion-chevron-right");
                var downElement = element.hasClass("ion-chevron-down");

                element.removeClass("ion-chevron-right");
                element.removeClass("ion-chevron-down");

                if (rightElement)
                    element.addClass("ion-chevron-down");
                if (downElement)
                    element.addClass("ion-chevron-right");
            }
        }
    });
});});

It sounds like cookies are what you want here. 听起来像cookie是您想要的。 Check out this W3C spec on cookies , no jQuery required! 查看Cookie上的W3C规范 ,无需jQuery!

Once you save the cookie, after a page reload just check the state of the cookie and display the menu based on that 保存Cookie后,在页面重新加载后,只需检查Cookie的状态并基于该状态显示菜单

I think your best option will be to consider using window.localStorage with a cookie fallback. 我认为您最好的选择是考虑将window.localStorage与cookie后备一起使用。 In fact, if you are using angular you may even consider using angular-local-storage which can be seen here . 实际上,如果您使用的是angular ,甚至可以考虑使用angular-local-storage ,可以在此处看到。

angular-local-storage provides the cookie fallback for you but this isn't too hard to implement yourself and just requires a check for whether window.localStorage is defined. angular-local-storage为您提供了cookie后备功能,但这并不太容易实现,只需要检查是否定义了window.localStorage

Hope that helps you out! 希望对您有所帮助!

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

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