0
votes

How add cookie to my controller correct?

It`s error when I use $cookie otherwise everything works.

angular.module('app', ['ngCookies']);
app.controller('loginCtrl', function ($scope, $http, $state, Authorization, $cookies) {
$cookies.put('expires', 'example');
});

Error

angular.js:13920 Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24cookiesProvider%20%3C-%20%24cookies%20%3C-%20loginCtrl at Error (native) at http://localhost:64757/Scripts/angular.min.js:6:412 at http://localhost:64757/Scripts/angular.min.js:43:174 at Object.d [as get] (http://localhost:64757/Scripts/angular.min.js:40:432) at http://localhost:64757/Scripts/angular.min.js:43:236 at d (http://localhost:64757/Scripts/angular.min.js:40:432) at e (http://localhost:64757/Scripts/angular.min.js:41:158) at Object.instantiate (http://localhost:64757/Scripts/angular.min.js:42:24) at http://localhost:64757/Scripts/angular.min.js:90:32 at q (http://localhost:64757/Scripts/AngularUI/ui-router.min.js:7:14338)

1
What error do you get?sp00m
@sp00m I added to my post :)Rafał Developer

1 Answers

1
votes

If you go to the url that the error gives you: https://docs.angularjs.org/error/$injector/unpr?p0=$cookiesProvider%20%3C-%20$cookies%20%3C-%20loginCtrl

Your angular app is unable to find your dependency. Did you install the angular cookies file? You can install it with:

npm install angular-cookies

OR

bower install angular-cookies

Also when you declare your angular app you should assign it to a variable then use it for your controllers.

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

app.controller('loginCtrl', function ($scope, $http, $state, Authorization, $cookies) {
    $cookies.put('expires', 'example');
});