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>
什麼是真正的問題,你的「去」按鈕被禁用,直到你填寫兩個輸入權? – jitender
嘗試codepen。這只是各自的Go按鈕被禁用。 –
如果你想防止他們覆蓋每一個你只需要將它們保存爲不同的變量。 –