javascriptangularjscookiescookiestore

Using cookieStore: Cannot read property "put" of undefined


For some reason I'm having trouble using cookieStore. I'm adding it to my app in routes.js with:

var myApp = angular.module('myApp', ['ngRoute', 'ui.bootstrap', 'ngCookies']);

and then trying to use it in my TabCtrl as such:

myApp.controller('TabsCtrl', ['$scope', function ($scope, $cookieStore) {
  $cookieStore.put('tab', '#/dashboard/summary');

and I get "Cannot read property 'put' of undefined"


Solution

  • You're only injecting $scope into your controller but expecting $scope and $cookieStore. Add $cookieStore to your dependencies and make sure you've got your closing brackets (square and regular). You might have this already but you haven't posted all your code!

    So change:

    myApp.controller('TabsCtrl', ['$scope', function ($scope, $cookieStore) {
    

    to...

    myApp.controller('TabsCtrl', ['$scope', '$cookieStore', function ($scope, $cookieStore) {