$cookies.get is not a function - Angular JS

806 views Asked by At

Designed two pages. Cookies is not set and showing error.

CDN:

<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular-cookies.js"></script>

Main Page - Set Cookies / cookies is not set

 var app = angular.module('myApp', ['ngCookies']);
 app.controller('LoginCtrl', ['$cookies', '$scope', '$http', '$window', '$location',  function($cookies,$scope, $http,  $window, $location) {
  $cookies.put('username', 'name');  
}

Second Page - Get Cookies

  var app = angular.module('myApp', [ 'ngTable', 'ngCookies' , 'ngResource']);
  app.controller('AccountMappingCtrl', ['$scope', '$http','$cookies', 'NgTableParams',  function($scope, $http, $filter, $cookies, NgTableParams) {  
    $scope.UsernameCookies = $cookies.get("username");
}

It is showing $cookies.get is not working.

1

There are 1 answers

0
Aref Zamani On

try following

$scope.UsernameCookies = $cookies[username];

if will not work inject '$cookieStore' in your controller and try following:

$cookieStore.get('username')