angularjscookiestore

AngularJS, $cookieStore.put is not a function


I am getting error - "$cookieStore.put is not a function when I am trying to save cookies in my app. I am using AngularJS 1.4.1 and cookies same ver.

var examinationApp = angular.module("examinationApp", ['ngCookies']);
examinationApp.controller("examinationCtrl", ['$scope', '$window', '$http',
'$interval','$cookieStore', function ($scope, $window, $http, $cookieStore,
modalDialog){
$scope.saveTestsToCookieStore = function () {
            $cookieStore.put('answeredTests',JSON.stringify($scope.data));
            $cookieStore.put('answeredQuestions',JSON.stringify($scope.answeredTests));
            $cookieStore.put('questionStopped',$scope.currentQN);
        }...

When I try to save cookies I an get error - TypeError: $cookieStore.put is not a function.

What am I doing wrong?


Solution

  • Your parameters aren't lining up...

    Compare your list of dependencies in the inline array annotation to the list of parameters in the function declaration. The lists of dependencies have to line up (same dependencies in the same order). (Basically, your controller function was trying to call put on $interval rather than on $cookieStore)

    What you have...

    examinationApp.controller("examinationCtrl", [
      '$scope', '$window', '$http', '$interval','$cookieStore', 
      function ($scope, $window, $http, $cookieStore, modalDialog) {
    

    What you need...

    examinationApp.controller("examinationCtrl", [
      '$scope', '$window', '$http', '$cookieStore', 'modalDialog', 
      function ($scope, $window, $http, $cookieStore, modalDialog) {