简体   繁体   中英

How can i use in memory database to my Ionic app

I am new to ionic app development and also In Memory database, how can i use in memory database to my ionic app. For My Ionic app am using AngularJS, HTML5 and CSS. I need Data transaction from My Ionic app to Inn memory Database. Please give me some useful links. Thanks In advance.

try this way..!

command to install local storage

bower install a0-angular-storage

in index.html

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

make service :

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;
});

in controller:

.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');
    };
})

one more simple way ..

you can use local storage , First make a angular factory , then use this angular factory in your ionic app controller , sample code is given below:

Angular Factory ::

 .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] || '{}');
       }
   }
}]);

Angular Controller :

 .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 is only for limited data. For large number of data, you better sqlite

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM