I'm trying to save data which the application gets from a http.get request and load it the next time the user opens the application without the device connected to the internet.
My data loads fine from the Http.get request but isn't saving or loading.
I'm not sure why my code isn't working. Here's my code:
.controller('announcementCtrl', function($scope, $http) {
$scope.data = [];
$scope.infiniteLimit = 1;
$scope.doRefresh = function() {
$http.get('http://www.example.com')
.success(function(data) {
$scope.data = data;
window.localStorage.setItem("data", JSON.stringify(data));
})
.error(function() {
$scope.data = data;
if(window.localStorage.getItem("data") !== undefined) {
$scope.data = JSON.parse(window.localStorage.getItem("data"));
}
});
}
Inside
error,$scope.data = data;will probably set$scope.datatoundefinedunless you have some globaldatavariable we don't know of. I'm guessing yourerrorfunction should be more like