(function() {
function uCtrl($uibModalInstance, $cookies) {
var saveUser = function(username) {
$cookies.put('blocChatCurrentUser', 'username');
};
var getUser = function() {
return $cookies.get('blocChatCurrentUser');
};
this.isValid = function() {
var currentUser = getUser();
if(!currentUser === undefined) {
currentUser = currentUser.replace(/^\s+/, '').replace(/\s+$/, '');
}
else if(currentUser === '' || currentUser === undefined) {
this.currentUserError = 'Username Cannot Be Empty';
this.currentUserErrorTrue = true;
this.setUsername.name = '';
return;
}
else {
$cookies.blocChatCurrentUser = currentUser;
saveUser(currentUser);
$uibModalInstance.dismiss();
}
};
}
angular
.module('blocChat')
.controller('uCtrl', ['$cookies', '$uibModalInstance', uCtrl]);
})();
<div>
<div class = "modal-header">
<h2 class = "modal-title">Set A Username</h2>
</div>
<div class = "modal-body">
<br>This name will appear when you send messages.</br>
<form name = "Username" ng-submit = "cookieThis.isValid()">
<div class = "input-group animated">
<input type = "text" class = "form-control finderBar" ng-model = "this.username" name = "username" ng-minlength = "1" required autofocus>
</div>
</form>
</div>
<div class = "modal-footer">
<button class="btn btn-primary" type="button" ng-click= "cookieThis.isValid()" ng-disabled = "this.username == null">Set Username</button>
</div>
</div>
所有其他文件都可以正常工作。 ModalInstanceCtrl和ModalCtrl都已经使用各自的.html文件进行了测试,并且可以正常工作。所以基本上,我试图获取用户提供的用户名并将其存储在cookie中。当然,有使用cookie的getUser()和setUser()方法。