![](/img/trans.png)
[英]Reloading data from the database for dropdownlist without refreshing whole page
[英]Is there a better way to get data from localStorage after storing the data without refreshing/reloading the page
我正在尝试制作一个可以将数据存储到 localStorage 的按钮。 存储数据后,我想在不刷新/重新加载页面的情况下获取数据
我用来解决这种情况的方法是,我在一个按钮上放了 2 个命令。 第一个命令是存储数据,第二个命令是使用 $scope 获取数据,以便于在页面上显示
这是我的代码
$scope.storeData = function(){
if(localStorage.getItem('value') === null){
// The value that will set to the localStorage
$scope.data = 'Selamat sore';
// To set the value on $scope.data to localStorage
localStorage.setItem('value', JSON.stringify($scope.data));
//To get the value and display it on the page
$scope.getData = JSON.parse(localStorage.getItem('value'));
}else{
$scope.getData = JSON.parse(localStorage.getItem('value'));
}
}
它确实有效,但也许有更好的方法来做到这一点
谢谢
我觉得这个更干净,更简单。
function MyController($scope) {
// if value is null a default 'Selamat sore' is set
$scope.data = localStorage.getItem('value') || 'Selamat sore';
$scope.saveData = function() {
localStorage.setItem('value', JSON.stringify($scope.data));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.