AngularJS: Cannot add cookies with $ cookieStore

I am trying to add a cookie using $ cookieStore

$scope.addSession = function() { console.log('adding session'); $cookieStore.put('session', 'value'); $scope.session= $cookieStore.get('session'); }; 

But it does not work with the following error

 Error: Can't find variable: $cooki addSession@http ://run.plnkr.co/61uMvzWlbCCsHRYM/script.js:8 @https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:6365 @https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:12987 $eval@https ://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:8057 $apply@https ://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:8137 @https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:12988 dispatch@http ://code.jquery.com/jquery-2.0.3.min.js:5 handle@http ://code.jquery.com/jquery-2.0.3.min.js:5 [native code] 

Demo / code can be found here.

Please let me know what I'm doing wrong here.

0
source share
3 answers

The problem was updating the page, it started working after the page was refreshed.

Please go to the working code here

-5
source

This should work

 var myApp = angular.module('myApp', ['ngCookies']); myApp.controller('CookieCtrl', function ($scope, $rootScope, $cookieStore) { $scope.addSession = function() { console.log('adding session'); $cookieStore.put('session', 'value'); $scope.session= $cookieStore.get('session'); }; } 
0
source

I found that I cannot use cookies via cookieStore in jQuery function. I use $ .cookie to control it. Of course, you need to serialize the json object.

0
source

All Articles