我是角色的初学者。我想使用ngCookie
进行登录。我使用ajax从PHP后端获取用户信息,如果我有用户名,则会显示注销按钮。这是我的代码:
$http({
method:'POST',
url:'login.php',
data:$.param(vm.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(Success);
function Success(response) {
$cookies.get('username') = response.data.username;
$cookies.put('username','username');
}
这是我的HTML
<div ng-show="username">
<li><a href="">Hello {{username}}</a></li>
<li><a href="">Log out</a></li>
</div>
错误:
angular.js:12520 ReferenceError:赋值中的左侧无效
答案 0 :(得分:0)
这可能是错误
$cookies.get('username') = response.data.username;
如果您想为用户名指定值,请尝试
// Setting a cookie for the user
$cookies.put('currentUser', response.data.username);
// Retrieving a cookie for the user
var currentUser = $cookies.get('currentUser');
对于退出按钮,您可以尝试
<body ng-controller="MainCtrl">
<button ng-if="user">Log Out</button>
</body>
angular.controller("MainCtrl",function($scope,$cookies){
$scope.user = $cookies.get("currentUser");
});