2

设计了两个页面。Cookies 未设置并显示错误。

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>

主页- 设置 Cookie/cookie 未设置

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

第二页- 获取 Cookie

  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");
}

它显示 $cookies.get 不起作用。

4

1 回答 1

1

尝试关注

$scope.UsernameCookies = $cookies[username];

如果无法在您的控制器中注入“$cookieStore”并尝试以下操作:

$cookieStore.get('username')
于 2017-09-13T04:28:12.117 回答