我正在尝试使用$ localStorage进行CRUD操作,我需要从$ localStorage中获取阵列

时间:2018-10-26 11:11:33

标签: html twitter-bootstrap angular-local-storage

  

之后,我在表中添加了一些记录,但未保留在$ localStorage中

var app = angular.module('myApp', ['ngStorage']);
		app.controller('myCtrl', function($scope, $localStorage){
			$localStorage.message = [{fname:'Nokia', lname:'Lumia'}];
			$scope.result = $localStorage.message;
			$scope.addRecord = function(){
			 if($scope.fnameVal != undefined && $scope.lnameVal!= undefined && $scope.lnameVal!= '' && $scope.fnameVal != ''){
			$scope.result.push({'fname':$scope.fnameVal, 'lname':$scope.lnameVal}); 
			//$localStorage.result.push({{'fname':$scope.fnameVal, 'lname':$scope.lnameVal}});
			}}
		});
<meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script>
    
    <body ng-app="myApp" class="container">
	<hr/>
        <div ng-controller="myCtrl">
		{{result}}
		 <!-- <select class="form-control"><option ng-repeat="x in result">{{x.fname}}</option></select><br/> -->
		 <table class="table table-bordered">
		 <tr>
		 <th>First Name</th>
		 <th>Last Name</th>
		 <th width="150">Action</th>
		 </tr>
		 <tr ng-repeat="x in result">
		 <td>{{x.fname}}</td>
		 <td>{{x.lname}}</td>
		 <td><button ng-click="deleteRecord();" class="btn btn-default">Delete</button></td>
		 </tr>
		 <tr>
		 <td><input type="text" ng-model="fnameVal" class="form-control"/></td>
		 <td><input type="text" ng-model="lnameVal" class="form-control"/></td>
		 <td><button ng-click="addRecord();" class="btn btn-default btn-block">Add</button></td>
		 </tr>
		 </table>
		 </div> 
    </body>

0 个答案:

没有答案