1

关键是我想通过一些下拉菜单和少量输入(目前)将一些数据多次保存到本地存储中。我可以从输入中获取所有值,并在 console.log 中看到它。我想将该输入作为对象保存到 localstorage 中(单击按钮 Spremi),然后在更改选项并再次单击以将另一个对象保存到 lcoalsotrage 等之后。

在此处输入图像描述

浏览.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>

控制器.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);

}

})
4

2 回答 2

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

}
于 2016-03-09T07:40:37.573 回答
0

可以使用ngStorage,请参考https://github.com/gsklee/ngStorage

    angular.module('app', [
        'ngStorage'
    ]).controller('Ctrl', function($scope, $localStorage, $sessionStorage){

   // put your code here!

});

您也可以参考如何在 angularjs 中使用 ngStorage

于 2016-03-09T07:59:30.057 回答