在坚持角跨页面的刷新数据 [英] Persisting data across page refresh in Angular

查看:146
本文介绍了在坚持角跨页面的刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angular.module('eventTracker', [])

.controller('MainCtrl', ['$scope', 'Event', function($scope, Event){
    $scope.eventData = {}
    $scope.onSubmit = function(){
        Event.Add($scope.eventData)
        $scope.eventData = {}
    }
}])

.factory('Event', function(){
    if (!eventList) {
        var eventList = []
    }
    return {
        Add: function(event){
            eventList.push(event)
        },
        List: eventList
    }
})

大家好,我想通过一个页面刷新坚持EVENTLIST,和我喜欢的一些反馈,为什么这可能发生!我宁可不使用cookie,本地存储,或rootscope。我以为我的服务已正确设置了,但我想不会。任何帮助将大大AP preciated!一切完美除非我刷新页面,我失去所有的previous数据。

Hi all, I am trying to persist eventList through a page refresh, and I'd love some feedback as to why that might be happening! I'd rather not use cookies, localstorage, or rootscope. I thought my service was set up correctly, but I guess not. Any help would be greatly appreciated! Everything works perfectly except when I refresh the page, I lose all previous data.

推荐答案

您选择跨页面的状态真的归结为:

Your options for cross page state really boil down to:

服务器端 - 即坚持/通过ajax从服务器获取

Server side - i.e. persist/retrieve from the server via ajax

客户端:

  • Cookies
  • Other state stores such as flash (obsolete), HTML 5 local storage, session storage
  • Wrap the above in 3rd party library such as http://amplifyjs.com/api/store/

如果它是非常少量的状态和上面的选项是不适合你可以连接code在URL中的状态(例如,当您更改页面的查询字符串参数),但是这是一个大量的工作时间超过最琐碎的场景。

If it's very small amounts of state and the above options aren't suitable you could encode the state in URLs (e.g. as a query string parameter when you change page) but that's a lot of work for more than the most trivial scenario.

角应用程序/范围/ rootscope中的所有内容都会丢失刷新页面。

Everything within the angular app/scope/rootscope will be lost on a page refresh.

这可看着为好事,因为它给你一个机会,以清除浏览器内存。 SPA的应用是伟大的,但enourmous SPA的应用进行了大量的膨胀和一个坏的脚本将整个申请下来的风险。打破一个SPA成几个小的应用程序有很多好处为它去。

This can be looked on as a "good thing" as it gives you a chance to clear out the browser memory. SPA applications are great, but enourmous SPA applications carry a lot of bloat and risks of a single bad script bringing the entire application down. Breaking a SPA up into several mini applications has a lot of benefits going for it.

这篇关于在坚持角跨页面的刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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