简体   繁体   English

如何在Angular中刷新页面后检索本地存储值?

[英]How to retrieve the local storage value after refresh the page in Angular?

I put the input values into web local storage. 我将输入值放入Web本地存储中。

But my need is, to access the local storage value into the ng-model. 但我需要的是,将本地存储值访问到ng-model中。

After click the submit button, local storage values to be append in table row. 单击“提交”按钮后,要在表行中追加本地存储值。

I tried but it doesnot happen. 我试过但它没有发生。

Please refer the below code. 请参考以下代码。

 var helloApp = angular.module("helloApp", ["ngStorage"]); helloApp.controller("CompanyCtrl", function($scope, $localStorage) { $scope.companies = [{ 'name': 'Xerago Technologies', 'employees': 125000, 'headoffice': 'Bangalore' }, { 'name': 'Cognizant Technologies', 'employees': 100000, 'headoffice': 'Bangalore' }, { 'name': 'Wipro', 'employees': 115000, 'headoffice': 'Bangalore' }, { 'name': 'Tata Consultancy Services (TCS)', 'employees': 150000, 'headoffice': 'Bangalore' }, { 'name': 'HCL Technologies', 'employees': 90000, 'headoffice': 'Noida' }, ]; $scope.addRow = function() { $scope.message = { 'name': $scope.name, 'employees': $scope.employees, 'headoffice': $scope.headoffice }; $scope.companies.push($localStorage.message); console.log($localStorage.message); }; }); 
 <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script> <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script> <div ng-app="helloApp" ng-controller="CompanyCtrl"> <table class="table"> <tr> <th>Name </th> <th>Employees </th> <th>Head Office </th> </tr> <tr ng-repeat="company in companies"> <td>{{company.name}} </td> <td>{{company.employees}} </td> <td>{{company.headoffice}} </td> </tr> </table> <form class="form-horizontal" role="form" ng-submit="addRow()"> <div class="form-group"> <label class="col-md-2 control-label">Name</label> <div class="col-md-4"> <input type="text" class="form-control" name="name" ng-model="name" /> </div> </div> <div class="form-group"> <label class="col-md-2 control-label">Employees</label> <div class="col-md-4"> <input type="text" class="form-control" name="employees" ng-model="employees" /> </div> </div> <div class="form-group"> <label class="col-md-2 control-label">Headoffice</label> <div class="col-md-4"> <input type="text" class="form-control" name="headoffice" ng-model="headoffice" /> </div> </div> <div class="form-group"> <div style="padding-left:110px"> <input type="submit" value="Submit" class="btn btn-primary" /> </div> </div> </form> </div> 

Now i can able to push the input values in table and also available in local storage.. 现在我可以在表中推送输入值,也可以在本地存储中使用。

But when i refresh the page, the last updated data hides. 但是当我刷新页面时,最后更新的数据会隐藏。 I think i need to know how to get. 我想我需要知道如何获得。

Please help on this. 请帮忙。

Try this : as local storage will not work here, so you need to check from demo link at out side : So try this link : DEMO 试试这个:因为本地存储在这里不起作用,所以你需要从外面的demo链接检查:所以试试这个链接: DEMO

  var helloApp = angular.module("helloApp", []); helloApp.controller("CompanyCtrl", function($scope, $window) { $scope.companies = JSON.parse($window.localStorage.getItem('compnyData')); console.log($scope.companies); $scope.addRow = function() { $scope.message = { 'name': $scope.name, 'employees': $scope.employees, 'headoffice': $scope.headoffice }; $scope.name = ''; $scope.employees = ''; $scope.headoffice = ''; var tempCompanies = []; var tempData = JSON.parse($window.localStorage.getItem('compnyData')); console.log(tempData); if (tempData != null) { console.log('not null'); tempData.push($scope.message); $window.localStorage.setItem('compnyData', JSON.stringify(tempData)); $scope.companies = tempData; } else { $window.localStorage.setItem('compnyData', JSON.stringify([$scope.message])); $scope.companies = [$scope.message]; } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <div ng-app="helloApp" ng-controller="CompanyCtrl"> <table class="table"> <tr> <th>Name </th> <th>Employees </th> <th>Head Office </th> </tr> <tr ng-repeat="company in companies"> <td>{{company.name}} </td> <td>{{company.employees}} </td> <td>{{company.headoffice}} </td> </tr> </table> <div class="form-group"> <label class="col-md-2 control-label">Name</label> <div class="col-md-4"> <input type="text" class="form-control" name="name" ng-model="name" /> </div> </div> <div class="form-group"> <label class="col-md-2 control-label">Employees</label> <div class="col-md-4"> <input type="text" class="form-control" name="employees" ng-model="employees" /> </div> </div> <div class="form-group"> <label class="col-md-2 control-label">Headoffice</label> <div class="col-md-4"> <input type="text" class="form-control" name="headoffice" ng-model="headoffice" /> </div> </div> <div class="form-group"> <div style="padding-left:110px"> <input type="button" ng-click="addRow()" value="Submit" class="btn btn-primary" /> </div> </div> </div> 

you need to use set and get properties for localstorage to save and retrive your data. 您需要使用set和get属性进行localstorage来保存和检索数据。

assign your new data to message 将新数据分配给消息

 $scope.message = {'name':$scope.name, 'employees': $scope.employees, 'headoffice':$scope.headoffice};

to set the data in localstorage..... 在localstorage中设置数据.....

localStorage.setItem("newData",$scope.message);

to get the data on click..... 获取点击数据.....

localStorage.getItem("newData");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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