0

I am unable to get the cookies in Angular.js. The code which I used is

angular.module('Routes', ['ngRoute','reviewService']).config(['$routeProvider','$locationProvider','$cookies','$cookieStore',
function($routeProvider, $locationProvider,$cookies,$cookieStore) {
var user=$cookies['username']
if(user !='undefined')
    {
    $routeProvider
       .when('/', {
         templateUrl: '/view/home.html',
         controller: 'myController'
    })
............

The error which I am getting in browser console is

Error: [$injector:modulerr] Failed to instantiate module Routes due to:
Error: [$injector:unpr] Unknown provider: $cookies
........
8
  • is $username exist in cookies?? Commented Jan 6, 2015 at 3:56
  • Yes. I is available. Commented Jan 6, 2015 at 3:58
  • I think you should use (user !== undefined) Commented Jan 6, 2015 at 4:00
  • The error which I am getting in browser console is Error: [$injector:modulerr] Failed to instantiate module Routes due to: Error: [$injector:unpr] Unknown provider: $cookies Commented Jan 6, 2015 at 4:01
  • 1
    Unknown provider: $cookies tells you that you didn't load ngCookies module in your app Commented Jan 6, 2015 at 4:05

2 Answers 2

1

Finally this code is working fine..

angular.module('Routes',['ngRoute','reviewService','ngCookies']).config(['$routeProvider','$locationProvider',
function($routeProvider, $locationProvider) {
 var $cookies;
  angular.injector(['ngCookies']).invoke(function(_$cookies_) {
    $cookies = _$cookies_;
  });    
var user=$cookies['username']
console.log("user name:",user);
Sign up to request clarification or add additional context in comments.

Comments

0
angular.module('Routes', ['ngRoute','reviewService', 'ngCookies'])

$cookies is in ngCookies module, you need to import. Don't forget to add <script> tag in html.

3 Comments

@Jayanth did you add <script src="angular-cookies.js"> in your html?
My bad. $cookies is a service instead of a provider and you can only use providers in module.config.
Sorry I don't know how to either. Maybe you can use the raw cookie instead of $cookies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.