Is there a better way to get data from localStorage after storing the data without refreshing/reloading the page

I’m trying to make a button that could store the data to the localStorage. After storing the data, I want to get the data without refreshing/reloading the page

The way I use to fix this case is, I put 2 commands on a button. The first command is to store the data, and the second command is to get the data using $scope to make it easy to display it on the page

Here is my code

$scope.storeData = function(){
    if(localStorage.getItem('value') === null){
        $scope.data = 'Selamat sore';
        localStorage.setItem('value', JSON.stringify($scope.data));
        $scope.getData = JSON.parse(localStorage.getItem('value'));
    }else{
        $scope.getData = JSON.parse(localStorage.getItem('value'));
    }
}

It’s actually working, but maybe there is a better way to do this

Thanks

Source: AngularJS Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.