我创建了一个Plunkr。我点击按钮时按下项目,按钮颜色将变为红色,当点击同一个按钮时,按钮的颜色将变为绿色,将项目拼接在阵列中。
问题是存储完美。按钮$ localStorage.tableArray不使用所选的ng-options。
当我刷新按钮的页面颜色时,也会使用ng-storage和ng-options固定。
1st plunker is with ng-storage, 2nd plunker is with arrays 请帮帮我。
var app = angular.module('plunker', ["ngStorage"]);
app.controller('MainCtrl', function($scope,$localStorage) {
$scope.tablelist = [{"tablename":"t1"},{"tablename":"t2"}];
if($localStorage.tableArray === undefined){
$localStorage.tableArray = []
}
if($localStorage.tableslist === undefined){
$localStorage.tableslist = []
}
$scope.getTable=function(table) {
table.btnClass = table.btnClass == "btn-danger" ? "btn-success" : "btn-danger"
var exists = false;
angular.forEach($localStorage.tableArray, function (list,$index) {
if ((list.tablename == table.tablename)) {
console.log(list.tablename);
console.log(table.tablename);
exists = true;
$localStorage.tableArray.splice($index,1);
$scope.$storage=$localStorage.tableArray
$localStorage.tableArray.splice($index,1);
return false;
}
});
if(!exists) {
$localStorage.tableslist.push(table);
$localStorage.tableArray = $localStorage.tableslist;
console.log($localStorage.tableArray)
table.color = "red"
}
}
});