2

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.

Monisha A
  • 65
  • 9
  • What exactly is the error message you're seeing? – skirtle Sep 13 '17 at 04:13
  • In first page i dint get any error. But while I am checking the via IF statement it is saying cookies not set. $cookies.put('username', 'name'); if($cookies.get('username')) { alert("Success"); } else { alert("No Cookies"); } – Monisha A Sep 13 '17 at 04:15
  • in the second page, I am getting $cookies.get is not a function error. – Monisha A Sep 13 '17 at 04:16
  • Maybe because `'$filter'` is missing from the array on that second page? – skirtle Sep 13 '17 at 04:22
  • var app = angular.module('myApp', [ 'ngTable', 'ngCookies' , 'ngResource']); app.controller('AccountMappingCtrl', ['$scope', '$http','$cookies', '$filter', 'NgTableParams', function($scope, $http, $cookies, $filter, NgTableParams) { It is not working. – Monisha A Sep 13 '17 at 04:31

1 Answers1

1

try following

$scope.UsernameCookies = $cookies[username];

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

$cookieStore.get('username')
Aref Zamani
  • 2,023
  • 2
  • 20
  • 40