使用角度js

时间:2015-12-04 08:54:52

标签: javascript jquery angularjs checkbox

我有一个复选框,当复选框被选中时,显示一个div,在取消选中状态div被隐藏,它运行良好,但当我选中复选框并刷新页面时它变为未选中状态,我怎么能保持页面刷新时的复选框状态,对不起英文不好,提前谢谢。

角度代码:

myApp.controller("expressionController", function ($scope) {
// Show or Hide Div

$scope.showHideDiv = function () {
if ($scope.chkStatus) {
$scope.showhideprop = true;
}
else {
$scope.showhideprop = false;
}
}
});

HTMLcode:

<form id="form1">
<div data-ng-app="sampleapp" data-ng-controller="expressionController">
Show or Hide Div: <input type="checkbox" name="tarn" ng-model="chkStatus" ng-change="showHideDiv()" /> <br /><br />
<div style="padding:10px; border:1px solid black; width:30%; font-weight:bold" ng-show='showhideprop'>Hi Welcome to Angularjs... Hello World</div>
</div>
</form>

2 个答案:

答案 0 :(得分:0)

您可以监控page before unload event并将其添加到localstorage

答案 1 :(得分:0)

您可以使用localStorage:

$scope.showhideprop = localStorage.getItem('showhideprop') || false;

现在在函数中调用ngChange时将当前值设置为localStorage:

$scope.showHideDiv = function () {
   if ($scope.chkStatus) {
     $scope.showhideprop = true;
   } else {
     $scope.showhideprop = false;
   }
   localStorage.setItem('showhideprop', $scope.showhideprop);
}