简体   繁体   中英

How to add the objects into an array stored in local storage using angular js?

I am trying to do use the angular push function but it is not working.

i am storing form values into local storage every time it will be replacing the old object with new object...

I want to add strings (or objects) into an array.

Below is my HTML code:

<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>

Below is my js code:

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;
    }

}]);

The data will stored local storage

But everytime I click the button it overwrites the data in my localstorage.

Does anybody know how to do this?

You have to clear user object first after push in userArray as

$scope. user = {};

Here is the link Jsfiddle demo

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM