2015-05-20 68 views
0

我是angularjs和phonegap的新手。我试图在页面上构建一个应用程序,用户可以从9个类别中选择一个类别。我将一个复选框放在每个类别名称旁边以供用户选择类别。选择几个类别后(通过选中相应的复选框),用户可以按保存按钮来保存类别。我的问题是如何保存所有这些类别,以及如何检索它们?保存并从angularjs本地存储检索保存的复选框值

这是我到目前为止的代码

<!--html code --> <!-- the items here the titles of the categories pulled from a data.js file--> 
<div ng-repeat="row in items | partition:3"> 
<div ng-repeat="item in row""> 
<input type="checkbox" ng-model="(item.title)"> 
<div>{{item.title}}</div> 

<ons-button modifier="large" ng-click="savecategories()">Ready</ons-button> 

/* angularjs代码*/

app.controller('categoryController', function($scope) { 
$scope.savecategories = function() { 
    if ($scope.category1) 
    { 
     localStorage.setItem('category1', true); 
    } 
    else { 
     localStorage.setItem('category2', false); 
    if ($scope.category2) 
    { 
     localStorage.setItem('category2', true); 
    } 
    else { 
      localStorage.setItem('category2', false); 
     } 

    /*and so on */ 
     } 
    }); 

我想看看它是否是通过把下面这行代码的工作,这样我就得到一个警报,但没有运气。

var national = localStorage.getItem('category1'); 
window.alert(national); 

我不知道我在做什么错。任何帮助将不胜感激。

+0

添加警报在这SaveCategories和检查,如果这个函数被调用,我认为它savecategories – Reena

+0

我不好,拼写错了这里。更正它。 :) – workthat

+0

有什么我不明白,你能告诉我什么是$ scope.category1? – carton

回答

0

谢谢大家。我终于明白了。我只是添加了一个函数,每次选中或取消选中复选框时都会调用该函数。

<input type="checkbox" ng-model="item.value" name="item.title" ng-change="savecategories()"> 

在我的js文件,我这样做:

app.controller('HomeController', function($scope, Data, localStorageService) { 

    $scope.items = Data.items; 

    if (localStorageService.get('items')) { 
     $scope.items = localStorageService.get('items'); 
    } 

    $scope.SaveCategories = function() { 
    localStorageService.clearAll(); 
    localStorageService.add('items',$scope.items); 

    }