简体   繁体   English

重新加载后状态仍然存在

[英]State persist after reload

I have a star icon that i want it to turn yellow every-time a user press it but i don't know how to persist this color to it's related object after reload.. 我有一个星形图标,我希望它在用户每次按下时变为黄色,但是我不知道如何在重新加载后将该颜色保留到与其相关的对象上。

This my javascript 这是我的javascript

$scope.favorite = function (idea) {
        $scope.newFav.ideaId = idea.id;
        $(document).on('click', '.box-btn', function() {
            $(this).find('.uiIconStar').toggleClass('reeed').toggleClass('uiIconStarBlank uiIconColorStarYellow');
        });
    };
    $scope.saveFavorite = function(idea) {
        $scope.newFav.ideaId = idea.id;
        $http({
            data : $scope.newFav,
            method : 'POST',
            headers : {
                'Content-Type' : 'application/json'
            },
            url : ideaFrontContainer.jzURL('IdeaFrontController.SaveFavorite')
        }).then(function successCallback(data) {
            $scope.loadFavorites();
        }, function errorCallback(data) {
            //  $scope.setResultMessage($scope.i18n.defaultError, "error");
        });


    };

You can use localStorage to store a flag in the browser. 您可以使用localStorage在浏览器中存储标志。

The read-only localStorage property allows you to access a Storage object for the Document's origin; 只读的localStorage属性允许您访问文档来源的存储对象; the stored data is saved across browser sessions. 存储的数据跨浏览器会话保存。 localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed. localStorage与sessionStorage相似,不同之处在于,尽管localStorage中存储的数据没有到期时间,但页面会话结束时(即关闭页面时),存储在sessionStorage中的数据将被清除。

Then, on page load, simply check whether the flag is set and adjust the color accordingly. 然后,在页面加载时,只需检查标志是否已设置并相应地调整颜色。

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

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