localStorage的角度和对象

localStorage的角度和对象

问题描述:

这是我的代码的简化版本,但我需要能够将每个输入字段的输出存储在localStorage中,而不会覆盖彼此。localStorage的角度和对象

自动取款机,它甚至没有工作。我必须填写两个输入才能保存任何内容。

var app = angular.module("app", []); 
 

 
app.controller("IndexController", ["$scope", function($scope) { 
 

 
    this.list = { 
 
    "mandag": {}, 
 
    "tirsdag": {} 
 
    }; 
 

 

 
    //LOCALSTORAGE 
 
    this.Save =() => { 
 
    localStorage.setItem("Ret", JSON.stringify(this.list.mandag)); 
 
    } 
 

 
    this.addFood = (day, title) => { 
 
     this.list[day] = { 
 
     "food": title, 
 
     "ingredients": [] 
 
     }; 
 
     console.log(this.list); 
 

 
    } 
 

 
    
 
    this.removeFood = (day) => { 
 
    this.list[day].food = ""; 
 
    } 
 

 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <title></title> 
 
    </head> 
 

 
    <body ng-app="app" > 
 
    <div ng-controller="IndexController as vm"> 
 
     <div ng-repeat="(day, item) in vm.list track by $index"> 
 
      <h3>{{day}}</h3> 
 
      <form class="" name="ret" ng-submit="vm.addFood(day, vm.newFood[$index]);vm.newTodo[$index] = ''"> 
 
      <input placeholder="Ædelse" type="text" id="foo" ng-model="vm.newFood[$index]" ng-disabled="item.food" value="" required/> 
 
      <button ng-disabled="ret.$invalid" ng-click="submitted=true; vm.Save()" ng-hide="submitted">Go</button> 
 
      </form> 
 
      <button ng-click="vm.removeFood(day); submitted=false" ng-show="submitted">X</button> 
 
     </div> 
 

 
    </div> 
 

 
    
 
    </body>

Codepen version

+0

什么是真正的问题,你的“去”按钮被禁用,直到你填写两个输入权? – jitender

+0

尝试codepen。这只是各自的Go按钮被禁用。 –

+0

如果你想防止他们覆盖每一个你只需要将它们保存为不同的变量。 –

我通过去除固定这个NG点击= “vm.Save()” 中的HTML和rearragning我的JavaScript一点:

this.Save =() => { 
 
     localStorage.setItem("Ret", JSON.stringify(this.list)); 
 
    } 
 

 
    this.addFood = (day, title) => { 
 
     this.list[day] = { 
 
     "food": title, 
 
     "ingredients": [] 
 
     }; 
 
     console.log(this.list); 
 

 
     this.Save(); 
 
    }