1

重點是我想通過一些下拉菜單和幾個輸入(現在)多次將一些數據保存到localstorage中。我可以從輸入中獲取所有值,並在console.log中看到它。我想保存輸入到localStorage的爲目標(點擊按鈕Spremi),和後更改選項以及一個更點擊到另一個對象保存到lcoalsotrage等多次將輸入添加到本地存儲

enter image description here

browse.html

<ion-view view-title="Browse"> 
    <ion-content> 
    <select id="vrstaribe" ng-model="selekt" ng-options="r as r for r in ribe" selected> 
    <option value="">Vrsta ribe</option> 
    </select> 
    <label class="item item-input"> 
    <input id="tezina" type="number" placeholder="Tezina"> 
    </label> 
    <label class="item item-input"> 
    <input id="mamac" type="text" placeholder="Mamac"> 
    </label> 
    <button class="button button-positive" ng-click="spremi()">Spremi</button> 
    </ion-content> 
</ion-view> 

contollers.js

.controller('SpremiCtrl', function($scope) { 
var ulov = {vrstaribe: '', tezina: '', mamac: '' }; 
var popisulova = []; 

$scope.ribe = ["Saran", "Stuka", "Som"]; 

$scope.spremi = function() { 

var vr = document.getElementById('vrstaribe'); 
var rib = vr.options[vr.selectedIndex].text; 
var tez = document.getElementById('tezina').value; 
var mam = document.getElementById('mamac').value; 
console.log("Riba : " + rib + '\n' + "Težina : " + tez + '\n' + "Mamac : " + mam); 
ulov.vrstaribe = rib; 
ulov.tezina = tez; 
ulov.mamac = mam; 
popisulova.push(ulov); 
console.log(ulov); 

localStorage.setItem('ulov', JSON.stringify(ulov)); 
var vrati = localStorage.getItem('ulov'); 

//console.log('Ulov: ', JSON.parse(vrati)); 
console.log(ulov); 

} 

}) 
+0

它現在拋出什麼樣的錯誤? –

回答

0

這將解決你的問題,但你有很多其他的問題,你是不是編碼一種「有角度的方式」。

$scope.spremi = function() { 

    var vr = document.getElementById('vrstaribe'); 
    var rib = vr.options[vr.selectedIndex].text; 
    var tez = document.getElementById('tezina').value; 
    var mam = document.getElementById('mamac').value; 
    console.log("Riba : " + rib + '\n' + "Težina : " + tez + '\n' + "Mamac  : " + mam); 
    ulov.vrstaribe = rib; 
    ulov.tezina = tez; 
    ulov.mamac = mam; 

    var fromStorage = localStorage.getItem('popisulova') || '[]'; 
    var vrati = JSON.parse(fromStorage); 
    vrati.push(ulov); 

    localStorage.setItem('popisulova', JSON.stringify(vrati)); 

    console.log(ulov); 

} 
相關問題