2014-07-02 22 views
0

我試圖在iOS上創建angularJS應用程序,我想將複選框上的數據保存到本地存儲中。如何在AngularJS應用程序中將數組數據保存到本地存儲?

現在我的複選框的信息數組

$scope.cuisineList = [ 
    { text: "Asian Cuisine", checked: true }, 
    { text: "Cafe", checked: true }, 
    { text: "Chinese Cuisine", checked: true }, 
    { text: "Dessert and Bekery", checked: true }, 
    { text: "Dim Sum", checked: false }, 
    { text: "Ice Cream", checked: false }, 
    { text: "Japanese Cuisine", checked: false }, 
    { text: "Kiwi Cuisine", checked: false }, 
    { text: "Korean Cruisine", checked: false }, 
    { text: "Thai Cuisine", checked: false }, 
    { text: "Vegeterian Cuisine", checked: false } 
]; 

和我的HTML5(我用標誌性的框架)

<ion-checkbox ng-repeat="item in cuisineList" 
        ng-model="item.checked" 
        ng-checked="item.checked" 
        ng-change="cuisineListChange()"> 
      {{ item.text }} 
     </ion-checkbox> 

當複選框什麼變化,它會執行cuisineListChange()功能,我想將$scope.cuisineList保存成。

請給我一個例子,如何使用本地存儲保存複選框。

謝謝。

回答

0

由於無法將對象存儲在localStorage或sessionStorage中,因此需要對它們進行序列化(因此字符串將保存到localStorage中,並在返回時需要進行解析)。

存儲,使用:

localStorage.setItem('cuisineList', JSON.stringify($scope.cuisineList)); 

從localStorage的取回數據,使用:

$scope.cuisineList = JSON.parse(localStorage.getItem('cuisineList')); 
0

這裏是一個有用的對象:

var LocalStorageManager = { 
    setValue: function(key, value) { 
     window.localStorage.setItem(key, JSON.stringify(value)); 
    }, 
    getValue: function(key) { 
     try { 
      return JSON.parse(window.localStorage.getItem(key)); 
     } catch (e) { 

     } 
    } 
}; 

要保存的數據:

LocalStorageManager.setValue("key",data); 

檢索該數據:

LocalStorageManager.getValue("key"); 
相關問題