Dyon
Dyon

Reputation: 195

Cookie-Issue in Angular

i try to store Data in Cookies in Angular. It doesnt seem to work somehow. I made this function to store the cookie

Store the cookie

 $scope.goLove = function(name,id){
        var thebestCity = name;
        var bestCityID   = id;
        alert("Du magst: " + thebestCity + " mit der ID: " + bestCityID);
        $cookies.put('favourite', thebestCity);
        var favouriteCity = $cookies.get('favourite');


        alert(favouriteCity);


    }

Since the alert works i would think the data is stored inside the cookie. So i tried to open it with another function:

Access the cookie (not working)

cityApp.controller('cookieReader', function($scope, $cookies){
    $scope.tellmeCookie = function($scope, $cookies){
        var cookieInfo = $cookies.get('favourite');
        alert(cookieInfo);

    }
});

Somehow the function keeps breaking.As soon as i put the $cookies.get inside there is no more response! Could you please tell me why? Thank you very much!

Upvotes: 0

Views: 196

Answers (1)

Teliren
Teliren

Reputation: 342

Firstly have you include angular-cookies.js

Has the module been included in your app

angular.module('app', ['ngCookies']);

On your example that isnt working it kind of looks like your trying to make your code minify safe you could change it to something like this

cityApp.controller('cookieReader', 
    ["$scope", "$cookies", function($scope, $cookies){
        $scope.tellmeCookie = function(){
            var cookieInfo = $cookies.get('favourite');
            alert(cookieInfo);

        }
    }]
);

Upvotes: 1

Related Questions