【问题标题】:How to push the json(object) to array in local storage angular js?如何将 json(object) 推送到本地存储角度 js 中的数组?
【发布时间】:2017-07-02 07:12:17
【问题描述】:

我正在尝试使用角度推送功能,但它不起作用。

每次用新对象替换旧对象时,我都会将表单值存储到本地存储中......

我想将字符串(或对象)添加到数组中。

下面是我的 HTML 代码:

<form name="myForm" novalidate>
    <p> Username:<br>
        <input type="text" name="user" ng-model="user.user" required>
        <span ng-show="myForm.user.$pristine"/>
    </p>
        <p>Email:<br>
            <input type="email" name="email" ng-model="user.email" required>
            <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
                <span ng-show="myForm.email.$error.required">Email is required.</span>
                <span ng-show="myForm.email.$error.email">Invalid email address.</span>
            </span>
        </p>

            <p> PhoneNum:<br>
                <input type="number" name="PhoneNum" ng-model="user.PhoneNum" required>
                <span ng-show="myForm.PhoneNum.$pristine"></span>
            </p>
                <p>Address:<br>
                    <input type="text" name="address" ng-model="user.address" required>
                    <span ng-show="myForm.address.$pristine"></span>
                </p>
                    <p>Password:<br>
                        <input type="Password" name="pass" ng-model="user.pass" required>
                        <span ng-show="myForm.pass.$pristine"  />
                    </p>
                        <p>
                            <input type="submit" ng-click="addItem(user)" ng-disabled="myForm.$invalid">
                        </p>

</form>
<pre>user = {{user | json}}</pre>
<table>
{{userdata}}
    <tr>
        <td>user</td>
        <td>Email</td>
        <td>Phone</td>
        <td>Address</td>
        <td>Password</td>
    </tr>
    <tr ng-repeat="x in userArray">
        <td>{{x.user}}</td>
        <td>{{x.email}}</td>
        <td>{{x.PhoneNum}}</td>
        <td>{{x.address}}</td>
        <td>{{x.pass}}</td>
    </tr>
</table>

下面是我的js代码:

var app = angular.module("myApp", ['ngRoute','ngStorage']);
app.controller("userCtrl",['$scope','$localStorage','$log',function($scope,$localStorage,$log){
    $scope.userArray=[];
    $scope.userdata=$localStorage.data;

    $scope.addItem=function(user){    
        //var current=$localStorage.data;
        //$scope.userArray=userArray.concat(user);
        $log.log($scope.userArray);
        $scope.userArray.push(user);
        $localStorage.data=$scope.userArray;
        $scope.userdata=$localStorage.data;
    }

}]);

【问题讨论】:

标签: javascript html angularjs ng-storage


【解决方案1】:
$scope.userArray = [];
    $scope.userdata = $localStorage.data;

    $scope.addItem = function(user) {
      $scope.userArray.push(user);
      $scope.user = {};
      $localStorage.data = $scope.userArray;
      $scope.userdata = $localStorage.data;
    }

【讨论】:

    【解决方案2】:

    在分配给localstorage 之前获取localstorage 数据。存储前使用JSON.Stringify,访问前使用JSON.prase

    $scope.userArray.push(user);
    
    var temp = JSON.parse($localStorage.data);// get it before pushing.
    
    temp.push($scope.userArray);//push to temp
    
    $localStorage.data =JSON.stringify(temp);//assing temp to localStorage
    
    $scope.userdata=$localStorage.data;
    

    【讨论】:

    • 我相信在这种情况下必须提到原因是您只能将字符串存储在本地存储中。
    • 我收到一个错误更新到您的代码...(意外的令牌 u 在 JSON 中的位置 0)@Dinesh
    • 位置 0 @Dinesh 的 JSON 中的意外令牌 u 和本地存储显示没有数据
    • var temp = JSON.parse($localStorage.data) || [];试试这条线
    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多