Hello
I am still noob for local storage about cannot show all after input and input data, please check my scripts below to know what’s wrong:
test.html
<ion-view view-title="Tester" ng-controller="testCtrl">
<div class="bar bar-subheader">
<div class="list list-inset">
<label class="item item-input">
<span class="input-label">Name</span>
<input type="text" placeholder="Name" ng-model="test.name">
</label>
<label class="item item-input">
<span class="input-label">Data</span>
<input type="text" placeholder="Data" ng-model="test.data">
</label>
<button class="button button-block button-positive" ng-click="testAdd()">
Add Item
</button>
</div>
</div>
<ion-content class="has-header has-subheader">
<ion-list>
<ion-item ng-repeat="test in testData" type="item-text-wrap">
<p>{{test.name}}</p>
<p>{{test.data}}</p>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
services.js
angular.module('directory.services', [])
.factory('riwayatvar', ['$window', function($window) {
return {
set: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
setObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key) {
return JSON.parse($window.localStorage[key] || '{}');
}
}
}]);
controllers.js
angular.module('directory.controllers', [])
.controller('testCtrl', ['$scope', 'testvar', function($scope, testvar) {
$scope.testdata = [];
$scope.testdata = angular.fromJson(localStorage.getItem("testvar"));
console.log($scope.testdata);
// Add an item function
$scope.testAdd = function() {
localStorage.setItem({content:$scope.test.name},{content:$scope.test.data});
console.log(testvar.get({content:$scope.test.name},{content:$scope.test.data}));
};
});
My another problem after click “add item” button show [object] [object] on console log.
I will appreciate if anyone could help me to solve this problem.