Dyon
Dyon

Reputation: 195

Storing a list of Items in Cookie in Angular

im am trying to store a list of items in a cookie.

For Testing-Examples i use a list of citys. It works so far but i always get the

SQLiteManager_currentLangue: and the XSRF-TOKEN: with it. I dont really have an idea how to get rid of them both.

Any suggestions?

  $scope.addToList = function(name,id) {

        var cityToAdd = name;
        var cityToAddID = id;

        // ADD A CITY TO THE COOKIE -> WORKS
        $cookies.put(cityToAddID, cityToAdd);

        // SHOW THE NEW CITY_LIST ->WORKS
        var allCitys = $cookies.getAll();
        console.log(allCitys);


        // PUT ALL INTO AN ARRAY -> WORKS
        var favouritesFromCookie = [];
        $.each(allCitys, function(index, value) {
            console.log(value);
            favouritesFromCookie.push(value);
        });

        // PUT THE ARRAY OF CITYS INTO A SCOPE_VARIABLE
        $scope.favouriteFinal = favouritesFromCookie;

            // GET RID OF THE LAST TWO ELEMENTS


    }

Upvotes: 2

Views: 929

Answers (1)

Tyler
Tyler

Reputation: 11499

You could give your own cookies a recognizable label and then grab that conditionally when you're compiling your array. Like so:

$cookies.put('city.' + cityToAddID, cityToAdd);

...

$.each(allCitys, function(index, value) {
    if (index.indexOf('city.') == 0) { favouritesFromCookie.push(value) }
});

Upvotes: 1

Related Questions