Nidheesh
Nidheesh

Reputation: 4562

Argument 'controller' is not a function, when ngCookies added

I am trying to import ngCookies in the following way in my controller. Im getting the below error when I added ngCookies.

Argument 'HomeCtrl' is not a function, got undefined

Controller.js:

angular.module('angularRestfulAuth', ['ngCookies']);
angular.module('angularRestfulAuth')
    .controller('HomeCtrl', ['$rootScope', '$scope', '$location','$cookies', '$localStorage', 'Main', function($rootScope, $scope, $location,$cookies, $localStorage, Main) {

Here is my service,

angular.module('angularRestfulAuth')
    .factory('Main', ['$http','$cookies', '$localStorage', function($http,$cookies, $localStorage){

my index html has ,

<body ng-app="angularRestfulAuth">

    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation" data-ng-controller="HomeCtrl">
    ....
    </div>

     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-animate.min.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-cookies.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-local-storage/0.6.0/angular-local-storage.min.js"></script>

Upvotes: 1

Views: 52

Answers (1)

JeanJacques
JeanJacques

Reputation: 1754

There isn't any problem with ngCookies, but if you want to use Angular Local Storage you need to inject it in your module :

angular.module('angularRestfulAuth', ['LocalStorageModule']);

And you can use the service in your controller or in your factory like this :

angular.module('angularRestfulAuth')
    .controller('HomeCtrl', ['localStorageService', function(localStorageService) {
    localStorageService.get(key);
    ...
}

angular.module('angularRestfulAuth')
    .factory('Main', ['localStorageService', function(localStorageService){
    localStorageService.get(key);
    ...
}

But I think you shouldn't use both, ngCookies and AngularLocalStorage, because they are both designed for the same thing, using browsers cookies (and also using local storage for AngularLocalStorage)

Here is a example of using cookies with AngularLocalStorage

Upvotes: 1

Related Questions