angularjs - Storing data in cache in an Ionic App -
i using external wordpress rest api content ionic app.
this provides 500 pages feeding wordpress site. if user doesn't have internet access when accessing app. there way can populate content in apps cache before app build there content view every page.
then perhaps later when internet access page can updated?
if goal store client-side , persistent data, can't use $cachefactory, caches data current session.
you can save data in localstorage.
make this:
factory
.factory('content', ['$http', content]) function content($http) { function getcontent(callback, url) { var articles = []; var article = {}; $http.get(url).success(function(response) { if (response.content[0].length > 0) { (var = 0; < response.content[0].length; i++) { article = { title:response.content[0][i].title, subtitle:response.content[0][i].subtitle } articles.push(article); } } else { // } }).error(function() { // }).then(function() { callback(articles); });; } return { getcontent: getcontent } }
controller
.controller('contentctrl', ['$scope', '$http', '$timeout', 'content', '$localstorage', function($scope, $http, $timeout, content, $localstorage) { $scope.showcontent = function() { var url = wordpressurl; $timeout(function() { content.getcontent(function(data) { $scope.getcontent = data; var contentlist = []; data.foreach(function(localy) { var onearticle = { title: localy.title, subtitle: localy.subtitle } contentlist.push(onearticle); }); window.localstorage.setitem("contentlocaly", json.stringify(contentlist)); // $localstorage.message = localstorage["contentlocaly"]; }, url); }, 1000); } // $scope.showcontent(); } ])
then can use in other controllers.
$scope.localyfeed = function() { $scope.wordpress = json.parse(localstorage["contentlocaly"]); console.log($scope.wordpress); // return $scope.wordpress; };
for checking internet connection can make this.
factory
.factory('checkinternet', function() { return function checkinternet() { var haveinternet= true; if (window.cordova) { if (window.connection) { if (navigator.connection.type == connection.none) { haveinternet= false; } } } return haveinternet; }; })
controller
.controller('examplectrl', ['$scope', '$http','checkinternet', function($scope, $http, checkinternet) { $scope.showsomething = function() { var haveinternet= checkinternet(); if (haveinternet== true) { // } else { // else } } } ])
Comments
Post a Comment