I using localstorage to save the location of images within the app. The code is shown below.
Now, when I execute the following steps:
> successfully adds an image to $scope.images
and localstorage
> successfully loads $scope.images
from localstorage
> successfully adds additional images to $scope.images and localstorage
> does not display the images in the DOM, even though the object $scope.images
is correctly loaded (same as before).
What is going on in step 4?
UPDATE: this is the error the console is giving to me:
https://docs.angularjs.org/error/ngRepeat/dupes?p0=image%20in%20images&p1=object:24&p2=%7B%22nativeURL%22:%22img%2Fionic.png%22,%22id%22:0.638037292053923,%22$$hashKey%22:%22object:24%22%7D
Code
html file
<ion-view view-title="Dashboard Other">
<ion-content style="text-align: center;">
<button class="button button-energized" ng-click="addImage()">
Add image
</button>
<button class="button button-energized" ng-click="saveImages()">
Save Images
</button>
<button class="button button-energized" ng-click="deleteImages()">
Delete All Images
</button>
<ion-list>
<ion-item class="item item-text-wrap">
<div ng-repeat="image in images"><img class="thumbnail" ng-src="{{image.nativeURL}}"/></div>
</ion-item>
<ion-item class="item item-text-wrap">
{{images}}
</ion-item>
</ion-content>
</ion-view>
controller
.controller('MainCtrl', function($scope, $state, $localstorage, $cordovaCamera, $cordovaFile, $cordovaImagePicker) {
// 1
$scope.images = $localstorage.getObject('images', '[]');
// x
$scope.saveImages = function() {
//TODO: prevent duplicates
$scope.images.push({
nativeURL: "img/ionic.png",
id: Math.random() // x
});
$localstorage.setObject('images', $scope.images);
$scope.images = $localstorage.getObject('images', '[]');
// x
// x
}
// other stuff
})
factory $localstorage
.factory('$localstorage', ['$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, fallBack) {
return JSON.parse($window.localStorage[key] || fallBack);
},
remove: function(key) {
$window.localStorage.removeItem(key);
console.log("Deleted key: " + key)
}
}
}]);