如何在使用本地存储刷新页面后保持选中或取消选中复选框?

时间:2016-12-07 10:52:33

标签: javascript html angularjs local-storage

如何在Angularjs中使用本地存储刷新页面后如何选中或取消选中复选框?

这里我写了一些代码:

<ul class="custom-checkbox">
                <li ng-repeat="type in typeoffeatures">
                    <input type="checkbox" id="{{type.value}}" ng-checked=type.flagset ng-click="toggleSelection1(type.value)" value="{{type.value}}" required/>
                    <label for="{{type.value}}"><div class='custom-chkbox'>
                            <span>{{type.value}}</span>
                        </div></label>
                </li>
            </ul>

JS代码:

   $scope.typeoffeatures = [{
           value : 'Authentication',
           flagset : true,
       },{
            value : 'Notification',
            flagset : false,
       },{
           value : 'Analytics',
            flagset : true,
       }];
  });

Codepen link here

1 个答案:

答案 0 :(得分:2)

我已对您的codepen进行了一些调整:

  • ng-click添加到复选框
  • ng-change更改为ng-true-value(更可靠)
  • 添加了ng-false-value<input type="checkbox" id="{{type.value}}" ng-model="type.flagset" ng-change="saveField(type.value, type.flagset)" ng-true-value="'true'" ng-false-value="'false'" required/> 来表示true和false为字符串

    public static class TagCount extends Reducer<Text,IntWritable,Text,IntWritable> {
    
        private IntWritable one = new IntWritable(1);
        private Text out = new Text();
    
        public void reduce(Text key, Iterable<Text> values, Context context) 
                             throws IOException, InterruptedException {
    
            List<String> sites = new ArrayList<String>();
            for (Text t : values) {
                sites.add(t.toString());
            }
    
            for (int i=0; i<sites.size()-1; i++) {
                for (int j=i+1; j<sites.size(); j++) {
                    out.set(sites.get(i) + "_" + sites.get(j))
                    context.write(out, one);
                }
            }
        }
    }
    

https://codepen.io/anon/pen/dOeKjj?editors=1010