尝试使用 ngCookies 读取 cookie。有一个饼干:
Name: cookie-name
Content:value
Host:localhost
控制器.js:
var myControllers = angular.module('myControllers', ['ngCookies']);
myControllers.controller('AboutController', [
'$scope',
'$location',
'$cookies',
function ($scope, $location, $cookies) {
$scope.test1 = $cookies.get('cookie-name');
$scope.test2 = '2';
}]);
html是:
<div class="jumbotron text-center">
<h1>About Page</h1>
<p>test1:{{ test1 }}</p>
<p>test2:{{ test2 }}</p>
</div>
但结果页面是:
About Page
test1:
test2:2
更新
我在 spring 控制器中创建了 cookie。
import javax.servlet.http.Cookie;
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public AuthenticationResponse logIn(
@RequestParam(value="username") String username,
@RequestParam(value="password") String password,
HttpServletResponse response)
{
/* Test setting cookie */
Cookie cookie = new Cookie("cookie-name", username);
cookie.setMaxAge(3600*10);
response.addCookie(cookie);
AccessToken token = userService.authenticateUser(username, password);
return new AuthenticationResponse(token.getEncodedToken(), token.getExpiryDate());
}
我不知道为什么无法成功读取 cookie。欢迎任何建议。谢谢。