我从AngularJS开始,并且遇到了我的第一个问题。
如何在localStorage中绑定或保存用户名和密码?重新加载页面或重新启动应用程序后,我的表单为空。
我正在使用此插件:https://github.com/grevory/angular-local-storage
HTML:
<input type="text" name="cy_username" id="cy_username" ng-model="username">
<input type="password" name="cy_password" id="cy_password" ng-model="password">
<button class="button button-block button-positive icon ion-edit icon-left" ng-click="saveSettings()">Einstellungen
speichern
</button>
<button class="button button-block button-assertive icon ion-trash-b icon-left" ng-click="resetSettings()">
Einstellungen löschen
</button>
控制器:
angular.module('starter.controllers', ['LocalStorageModule'])
.controller('SettingsCtrl', function ($scope, localStorageService) {
$scope.username = localStorageService.get('username');
$scope.password = localStorageService.get('password');
$scope.saveSettings = function () {
if (localStorageService.isSupported) {
localStorageService.set('username', $scope.username);
localStorageService.set('password', $scope.password);
} else {
console.error('localStorage is not supported!');
}
};
// doesn't work, nothing happens
$scope.resetSettings = function () {
$scope.username = '';
$scope.password = '';
localStorageService.clearAll();
};
});
答案 0 :(得分:2)
将用户凭据存储在localStorage中是一种非常糟糕的做法。 但我得到JSBIN为您的要求工作,它的工作原理.. 滚动到底部以获取JS代码,因为我将库包含在JS
之上PS:忽略js错误和警告。这些来自角度局部存储库