2014-04-28 97 views
1

我有這個複選框。保持複選框的狀態angularjs

<div ng-repeat="setting in settings"> 
    <input type="checkbox" name="setting.name" ng-model="setting.value">{{setting.name}} 

,並在控制器

$scope.settings = [{ 
      name: 'OrderNr', 
      value: '' 
     }, { 
      name: 'CustomerNr', 
      value: '' 
     }, { 
      name: 'CatalogName', 
      value: '' 
     }, { 
      name: 'OrderDate', 
      value: '' 
     }, { 
      name: 'OrderState', 
      value: '' 
     }]; 

當我點擊保存按鈕,我在本地存儲

$scope.saveSetting = function() { 
    var json = angular.toJson($scope.settings); 
    localStorageService.add('settings',json); 

}; 

我怎能調用該函數用於存儲數據我的設置,當我重新加載頁面?

回答

2

如何使用$ cookies。

看一看angularjs docs

基本上所有你需要做的是注入的服務在控制器的依賴,並把它作爲一個對象。

在你的控制器:

$scope.saveSetting = function() { 
    $cookies.settings = $scope.settings; 
}; 

,並使用它們:

$scope.loadSetting = function() { 
    $scope.settings = $cookies.settings; 
}; 

請讓我知道它是否適合你。

0

我會使用ngStorage指令。我在我的應用程序中使用它。您只需使用ngStorage將設置設置爲存儲空間,然後在控制器加載時通過ngStorage設置初始值。

https://github.com/gsklee/ngStorage

0

你只需要設置時$ scope.settings控制器負載。

var myCtrl = function($scope) { 
    if(localStorage.settings) { 
     $scope.settings = angular.fromJson(localStorage.settings); 
    } 
}