【问题标题】:How can i use in memory database to my Ionic app我如何在内存数据库中使用我的 Ionic 应用程序
【发布时间】:2016-01-10 02:32:03
【问题描述】:

我是 ionic 应用程序开发和内存数据库的新手,我如何将内存数据库用于我的 ionic 应用程序。对于我的 Ionic 应用,我正在使用 AngularJS、HTML5 和 CSS。我需要从 My Ionic 应用程序到 Inn 内存数据库的数据事务。请给我一些有用的链接。提前致谢。

【问题讨论】:

  • 什么是'内存数据库'..?你想store locally还是使用sqlite or pouchdb来存储..?
  • 是的,我想本地存储,有没有更好的解决方案帮助我
  • 你想存储什么数据..!喜欢..!它是大名单吗..?如果是这样,那么你应该使用任何一个 dB ..!
  • 简单数据,如用户详细信息列表。任何一个意味着?

标签: html css angularjs ionic in-memory-database


【解决方案1】:

尝试this方式..!

安装本地存储的命令

bower install a0-angular-storage

在 index.html 中

<script src="lib/angular-storage.min.js"></script>

提供服务:

angular.module('app', ['angular-storage'])
.factory('UserDetailsService', function ( store ,$rootScope) {
    var self = {};
    self.getUsers = function () {
        var users = store.get('_userList');
        if (users){
            return users;
        }else{
            return null;
        }
    };
    self.setUsers = function(UserList) {
        $rootScope.users = UserList;
        store.set('_userList', UserList);
    };
    return self;
});

在控制器中:

.controller('UserCtrl', function ($scope,  UserDetailsService,store) {
    //to store data..!
    $scope.doLogin = function () {
        $http.post('**** URL *****', $scope.loginData).
            success(function (response) {
                $scope.users = response.result;
                UserDetailsService.setUsers($scope.users);// call to service..!
            }).error(function (response) {
            });
    };
    //to get local store list
    $scope.users = UserDetailsService.getUser();
    //to remove local store data
    $scope.toRemoveLocalData = function () {
        store.remove('_userList');
    };
})

还有一个simple way..

【讨论】:

    【解决方案2】:

    你可以使用本地存储,首先制作一个angular factory,然后在你的ionic app controller中使用这个angular factory /strong>,示例代码如下:

    Angular 工厂 ::

     .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) {
                return JSON.parse($window.localStorage[key] || '{}');
           }
       }
    }]);
    

    角度控制器

     .controller('mainCtrl', function($scope, $localstorage) {
           // set data to $localstorage
           // you can use this json data anywhere in your app
           $localstorage.setObject('object_name', json_data);
    
    
    
          // get $localstorage data
          var json_data = $localstorage.getObject('object_name'); 
     });
    

    NB:: localstorage 仅用于有限的数据。对于大量数据,你最好 sqlite

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 2016-03-13
      • 2011-01-16
      • 2020-10-04
      • 2018-04-10
      • 2019-06-28
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多