刷新后如何保留localStorage值?

tho*_*olo 3 html javascript jquery local-storage angularjs

这是我的ctrl:

app.controller('ctrl', function ($window, $scope) {

    $scope.initData = [
        {
            firstName: "John",
            lastName: "Doe",  
        },
        {
            firstName: "Jane",
            lastName: "Doe",
        },
        {
            firstName: "John",
            lastName: "Smith",
        }
    ];

    $window.localStorage.setItem('initData', JSON.stringify($scope.initData));
    $scope.retrievedData = JSON.parse($window.localStorage.getItem('initData'));
    $scope.sortedType = 'firstName';
    $scope.sortedReverse = false;
    //Remove Rows and Update localStorage Key Values
    $scope.removeRow = function(row) {
        $scope.retrievedData.splice(row, 1);
        $scope.initData.splice(row, 1);
        $window.localStorage.setItem('initData', JSON.stringify($scope.initData));
    }
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<table class="table table-bordered table-striped table-hover">
                <thead>
                <tr>
                    <th>
                        <span ng-show="sortedType == 'firstName' && sortedReverse" class="fa fa-long-arrow-up"></span>
                        <span ng-show="sortedType == 'firstName' && !sortedReverse" class="fa fa-long-arrow-down"></span>
                        <span href="#" ng-click="sortedType = 'firstName'; sortedReverse = !sortedReverse" style="cursor:pointer;">First Name</span>
                    </th>
                    <th>
                        <span ng-show="sortedType == 'lastName' && sortedReverse" class="fa fa-long-arrow-up"></span>
                        <span ng-show="sortedType == 'lastName' && !sortedReverse" class="fa fa-long-arrow-down"></span>
                        <span href="#" ng-click="sortedType = 'lastName'; sortedReverse = !sortedReverse" style="cursor:pointer;">Last Name</span>
                    </th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="(k, v) in retrievedData | orderBy: sortedType: sortedReverse">
                <td>{{v.firstName}}</td>
                <td>{{v.lastName}}</td>
                <td>
                    <button class="btn btn-primary">Edit</button>
                    <button class="btn btn-primary" ng-click="removeRow();">Delete</button>
                </td>
            </tr>
            </tbody>
        </table>
Run Code Online (Sandbox Code Playgroud)

现在,我相信这很清楚。它是从分配的数据$scope.initDatalocalStorage,然后将其插值到HTML。该函数将removeRow()删除表中的行,并localStorage以最新操作对其进行更新。问题是每次我重新加载HTML时,初始localStorage数据都会被加载,而不是更新后的数据。我知道这是因为我在这里给它分配:$window.localStorage.setItem('initData', JSON.stringify($scope.initData)); 但我不知道刷新后如何保持更新。

请指教。

谢谢。

BiJ*_*BiJ 5

$window.localStorage.setItem('initData', JSON.stringify($scope.initData));每次刷新时,您的生产线都会重置数据。

将此行替换为:

if(!localStorage.getItem('initData')){
    $window.localStorage.setItem('initData', JSON.stringify($scope.initData));
}
Run Code Online (Sandbox Code Playgroud)

更重要的是,您根本不应该使用该行。从一个空的本地存储开始,仅从UI添加新条目。

上面的代码只运行一次,这是应用程序的第一次运行。

如果您想在每次列表为空时重新插入数据,请尝试以下操作

if(!localStorage.getItem('initData') || JSON.parse(localStorage.getItem('initData')).length === 0){
    $window.localStorage.setItem('initData', JSON.stringify($scope.initData));
}
Run Code Online (Sandbox Code Playgroud)

  • 然后,您应该添加条件检查以查看是否没有数据,然后仅插入。 (2认同)