2017-10-11 79 views
0

這是我的代碼的簡化版本,但我需要能夠將每個輸入字段的輸出存儲在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

如果你想防止他們覆蓋每一個你只需要將它們保存爲不同的變量。 –

回答

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(); 
 
    }