Angularjs 如何在ionic中离线和在线管理数据

Angularjs 如何在ionic中离线和在线管理数据,angularjs,ionic-framework,ionic,Angularjs,Ionic Framework,Ionic,我正在开发博客应用程序。我还想在SqlLite上保存文章列表。我需要一次获取所有博客。(拥有2000多个)博客 下面是我的控制器代码 var promise= userService.getArticles(); promise.then(function(data) { $scope.articles = data; }, function(error) { console.log('Failed for some reason:' + error); }); 工厂代码是

我正在开发博客应用程序。我还想在SqlLite上保存文章列表。我需要一次获取所有博客。(拥有2000多个)博客

下面是我的控制器代码

var promise= userService.getArticles();
promise.then(function(data) {
        $scope.articles = data;
}, function(error) {
  console.log('Failed for some reason:' + error);
});
工厂代码是 角度模块('starter.controllers')

这是重复的结果

我还需要将这些数据保存在sqllite中。我还想将数据显示为脱机

我不知道如何进行这项工作。请帮忙


谢谢

大多数脱机应用程序将本地存储用作缓存,并在有可用连接时进行更新

一个简单的方法是:

  • 从本地存储中获取项目并将其分配给$scope变量(这可能是未定义的)
  • 正常从服务器请求文章
  • 成功回调时,覆盖本地存储并重新分配范围变量 示例代码:

    // The article service
    
    myApp.factory('articles', function($q, $timeout, $window, loremIpsum) {
    
    
      // Initialize by grabbing the articles from the local cache (if any)
    
      var items = angular.fromJson($window.localStorage.getItem('myArticles'));
    
      // Construct our service
    
      var service = {
        items: items,
        refresh: function() {
    
          // Sync with the server
    
          var defer = $q.defer();
    
          // For this demo I'm using a timeout (this also allows for some artificial lag).
          // Replace this with your $http calls.
    
          $timeout(function() {
    
            // Here I'm generating some total random items that we're resolving
            // Also not needed in your app
    
            var c = 100, result = [];
            while (c--) {
              result.push({
                title: loremIpsum.getRandomLine(2),
                description: loremIpsum.getRandomLine(15)
              });
            }
    
            service.items = result;
            defer.resolve(result);
    
            // Store the returned result from the server in the local storage
    
            $window.localStorage.setItem('myArticles', angular.toJson(result));
    
          }, 500);
    
          return defer.promise;
        }
      };
    
      return service;
    });
    

    可以找到Plunker示例

    你能不能也给我提供示例代码给sggin或改写variabke
    // The article service
    
    myApp.factory('articles', function($q, $timeout, $window, loremIpsum) {
    
    
      // Initialize by grabbing the articles from the local cache (if any)
    
      var items = angular.fromJson($window.localStorage.getItem('myArticles'));
    
      // Construct our service
    
      var service = {
        items: items,
        refresh: function() {
    
          // Sync with the server
    
          var defer = $q.defer();
    
          // For this demo I'm using a timeout (this also allows for some artificial lag).
          // Replace this with your $http calls.
    
          $timeout(function() {
    
            // Here I'm generating some total random items that we're resolving
            // Also not needed in your app
    
            var c = 100, result = [];
            while (c--) {
              result.push({
                title: loremIpsum.getRandomLine(2),
                description: loremIpsum.getRandomLine(15)
              });
            }
    
            service.items = result;
            defer.resolve(result);
    
            // Store the returned result from the server in the local storage
    
            $window.localStorage.setItem('myArticles', angular.toJson(result));
    
          }, 500);
    
          return defer.promise;
        }
      };
    
      return service;
    });