javascriptangularjscookiesangular-cookies

Unknown provider: $cookies angularjs


I have tried everything I can think of and I am still receiving this error.

app.js:

angular.module('myApp', [
  'ngCookies'
]).
config(['$cookies', function($cookies) {
  var cookie = $cookies.getObject();
  console.log(cookie);
}])

index.html:

  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-cookies/angular-cookies.js"></script>

bower.json:

  "dependencies": {
    "angular": "~1.5.0",
    "angular-cookies": "~1.5.0"
  }

what could seriously be going wrong? I've tried looking at everything on stackoverflow/google regarding how to fix these bugs but I still receive

Unknown provider: $cookies

When I do not add $cookies into the .config() of my app.js file, I receive no error.


Solution


  • You are getting this error because you cannot inject a service into the configuration section as the services won't be initialized yet. So instead of $cookies, you should use $cookiesProvider inside .config block. And for some reason if you want to use $cookies inside your config, you can inject it manually. Give this a try

    app.config(function() {
      var $cookies;
      angular.injector(['ngCookies']).invoke(['$cookies', function(_$cookies_) {
        $cookies = _$cookies_;
      }]);
    });