我使用本地存储。当我第一次进入页面时,我获得了一个旧的本地存储值。当我刷新时,我得到当前值。我怎样才能避免这个错误?每次我只需要当前的本地存储值。我已经用这段代码附上了这一点。我没有安装ngstorage。有人能告诉我,我是否需要安装ngstorage?
.controller('SignInCtrl', [
'$scope','$http' ,'$location','$window',
function($scope,$http, $location,$window) {
$scope.signin = function()
{
$http.post('***', userdetails).success(function(data,response)
{
var nameResponse = JSON.stringify(data.Name);
window.localStorage.setItem("name",nameResponse);
})
}
}])
.controller('MyprofileCtrl', [
'$scope','$http' ,'$location','$window',
function($scope,$http, $location,$window) {
var name=$window.localStorage.getItem("name");
//console.log(name);
if(name == null)
{
$scope.loginstatus ="Sign Up With Us";
console.log($scope.loginstatus);
}else
{
$scope.loginstatus ='Welcome '+ name.replace(/\"/g, "");
console.log($scope.loginstatus);
}
}])
答案 0 :(得分:0)
是的,您必须使用ngstorage模块。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ngStorage/0.3.7/ngStorage.min.js"></script>
</head>
<body ng-app="myModule">
<div ng-controller="myCtrl">
<button ng-click="save()">Save</button>
<button ng-click="load()">Load</button>
<input type="text" ng-model="txt">
{{myData}}
</div>
</body>
<script>
var myModule = angular.module("myModule", ["ngStorage"]);
myModule.controller("myCtrl", function($scope, $localStorage) {
$scope.txt = "prabhu";
$scope.save = function() {
alert($scope.txt)
$localStorage.message = $scope.txt;
}
$scope.load = function() {
$scope.myData = $localStorage.message;
}
});
</script>
</html>
答案 1 :(得分:0)
不,不需要ngStorage,我希望在你的控制器中正确注入$ window的依赖项。
试试这个:
bannerViewActionDidFinish