how to store ng-repeat array in localstorage

662 views
Skip to first unread message

Srikar Shastry

unread,
Mar 20, 2014, 11:18:49 PM3/20/14
to ang...@googlegroups.com
Hello, 
I have base model as service which store the content into the localstorage (stackoverflow example, mine is almost the same):
app.factory('userService', ['$rootScope', function ($rootScope) {

    var service = {

        model: {
            name: '',
            email: '',
            "testArray": [
        {
            "first": 1,
            "second": 2,
            "third": "abc"
        },
        {
            "first": 4,
            "second": 5,
            "third": "xyz"
        }
        ]
        },

        SaveState: function () {
            sessionStorage.userService = angular.toJson(service.model);
        },

        RestoreState: function () {
            service.model = angular.fromJson(sessionStorage.userService);
        }
    }

    $rootScope.$on("savestate", service.SaveState);
    $rootScope.$on("restorestate", service.RestoreState);

    return service;
}]);
Now, I have ng-repeat with input type text on each repeat and has ng-model respectively. 
I'm trying to capture these ng-models in the above base model as ARRAY(not object) and then, store the object model in the localstorage.
How can I store ng-repeat array in the localStorage? Will the current approach work?
Thank you.

Tony pee

unread,
Mar 21, 2014, 12:14:40 AM3/21/14
to ang...@googlegroups.com
ng-reapeat will iterate over any object or array. How you store, hydrate, modify, load, save, this in the model is up to you. As long as you remember to call $scope.$apply after an async operation that is performed outside of angular, then it should be reflected in the ng-repeat. 

So yes, you can save/load data to localStorage, and if you replace your model that is referenced by the ng-repeat, it should update. 

is this what your asking?



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Tony Polinelli

Srikar Shastry

unread,
Mar 21, 2014, 1:02:02 AM3/21/14
to ang...@googlegroups.com
model = {
        firstObj: {
            a: '',
            b: '',
            c: '',
            d: {
                x: 0,
                y: 0,
                z: 0
            }
        },
        secondArray: [
            {
                 some: '',
                 thing:''
            }
         ],
        }

The model that I want to save on localstorage looks something like the above object.
Now, the "secondArray" is what goes to ng-repeat. Every ng-repeat has input field with ng-model "thing". Now, when all the "things" are filled and you do a console log, I get the model with only 1 array. (length of array = 1). The array is being over written with the last value. (ng-repeat has more than 10 entries). What am i doing wrong?

Tony pee

unread,
Mar 24, 2014, 4:40:30 PM3/24/14
to ang...@googlegroups.com
If you make a plunkr with your problem, i think we could easily trace the problem, as it doesnt seem like there should be an issue. 

cheers
Reply all
Reply to author
Forward
0 new messages