gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:48:10 26 4
gpt4 key购买 nike

我将输入值放入网络本地存储。

但我的需要是,将本地存储值访问到 ng-model 中。

点击提交按钮后,本地存储值将追加到表行中。

我试过了,但没有成功。

请引用下面的代码。

  

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>

现在我可以将输入值推送到表中,也可以在本地存储中使用..

但是当我刷新页面时,最后更新的数据隐藏了。我想我需要知道如何获得。

请帮忙解决这个问题。

最佳答案

试试这个:因为本地存储在这里不起作用,所以你需要从外面的演示链接检查:所以试试这个链接: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>

关于javascript - 在 Angular 中刷新页面后如何检索本地存储值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40237864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com