我希望您从 html 获取数据,调用 Web 服务并将数据存储在本地存储中。成功调用将数据存储在$http
本地存储中的函数。
setUserDetails = function(userData){
var username = "";
if(userData != null){
app.setInLocalStorage("loginName",userData.userName);}
}
其中 userData 就像一个对象包含您的 html 数据,
var userData= {
userName : $scope.userName,
};
userData.userName
从本地存储中获取
var userLoggedIn = app.retrieveFromLocalStorage("userName");
比较数据是否从服务器更改
if(userLoggedIn != null){
//call the service which gives dynamic response
//i hope your saving the success data in userData and userName is property which changes dynamically
var newUser = userData.userName;
if(angular.equals(userLoggedIn, newUser){
// maintain the same data in localstorage
//or
//No need to call any other function calls
}else{
//clear the old data from local storage
app.clearLocalStorage();
//storage the new dynamic data in local storage
//or
//call the new function calls if you need
app.setInLocalStorage("loginName",newUser)
}
}
在 app.js 文件中包含以下行来存储、检索、清除本地存储
setInLocalStorage : function(key , value) {
// Check browser support
if (typeof(Storage) != "undefined")
{
// Store
localStorage.setItem(key , value);
}
else
{
alert("Sorry, your browser does not support Web Storage...");
}
},
retrieveFromLocalStorage : function(key){
return localStorage.getItem(key);
},
clearLocalStorage : function(){
localStorage.clear();
}