gpt4 book ai didi

javascript - 如何绑定(bind)本地存储数据中的值?

转载 作者:行者123 更新时间:2023-11-28 01:42:21 27 4
gpt4 key购买 nike

我正在尝试使用 LocalStorage.setItem(key,value); 通过 localStorage 发布列表;然后我通过 LocalStorage.getItem(key) 收到了该数据;我的问题是如何绑定(bind)localStorage Data的值。

<div ng-controller="ContactController">
<form border="2">
<label>
Name</label>
<input type="text" name="name" ng-model="newcontact.name" />
<label>
Email</label>
<input type="text" name="email" ng-model="newcontact.email" />
<label>
Phone</label>
<input type="text" name="phone" ng-model="newcontact.phone" />
<br />
<input type="hidden" ng-model="newcontact.id" />
<input type="button" value="Save" ng-click="saveContact()" />
</form>
<table border="3">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
<th>
Phone
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td>
{{ contact.name }}
</td>
<td>
{{ contact.email }}
</td>
<td>
{{ contact.phone }}
</td>
<td>
<a href="#" ng-click="edit(contact.id)">edit</a> | <a href="#" ng-click="delete(contact.id)">
delete</a>
</td>
</tr>
</tbody>
</table>
<div>
<input type="button" ng-click="LoadProductDetails()" value="click" />
</div>
<table border="3">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
<th>
Phone
</th>
<th>
id
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in datalist track by $index">
<td>
{{ contact.name }}
</td>
<td>
{{ contact.email }}
</td>
<td>
{{contact.phone }}
</td>
<td>
{{contact.id }}
</td>

</tr>
</tbody>
</table>

从列表中我获取了那些绑定(bind)数据..?如何使用 javaScript 绑定(bind)数据?

最佳答案

Knockout.Localstorage

(function(ko){
// Wrap ko.observable and ko.observableArray
var methods = ['observable', 'observableArray'];

ko.utils.arrayForEach(methods, function(method){
var saved = ko[method];

ko[method] = function(initialValue, options){
options = options || {};

var key = options.persist;

// Load existing value if set
if(key && localStorage.hasOwnProperty(key)){
try{
initialValue = JSON.parse(localStorage.getItem(key))
}catch(e){};
}

// Create observable from saved method
var observable = saved(initialValue);

// Subscribe to changes, and save to localStorage
if(key){
observable.subscribe(function(newValue){
localStorage.setItem(key, ko.toJSON(newValue));
});
};

return observable;
}
})
})(ko);

Here is the sample using amplifyjs
Here is the sample in knockout.localstorage

如果你想使用Angular js,我的选择是ngStorage here is Demo

关于javascript - 如何绑定(bind)本地存储数据中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20756349/

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