如何绑定本地存储数据中的值?

时间:2013-12-24 06:58:52

标签: javascript knockout.js local-storage

我正在尝试使用LocalStorage.setItem(key,value)通过localStorage发布列表; 然后我通过LocalStorage.getItem(key)收到了这些数据; 我的问题是如何绑定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>

从列表中我得到那些绑定数据..?如何使用javaScript绑定数据?

1 个答案:

答案 0 :(得分:1)

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

如果您想使用角度js,我的选择是 ngStorage here is Demo