Store and read settings permanently on local filesystem in Cordova/Ionic projects.
When you run your application in device ng-cordova-settings uses the local filesystem (with cordova-plugin-file) and the system logs (with console.log).
When you run your application in browser with ionic serve
the ng-cordova-settings uses browsers localStorage and the browser console (with console.log).
- ngCordova ( required version v0.1.14-alpha )
- cordova-plugin-file
Add cordova plugin
$ cordova plugin add cordova-plugin-file
Install ng-cordova-settings manually, or with ionic:
$ ionic add ng-cordova-settings
Include ng-cordova-settings.min.js and ng-cordova.min.js in your index.html file before cordova.js and after your AngularJS / Ionic file (since ngCordova depends on AngularJS).
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<script src="lib/ng-cordova-settings/dist/ng-cordova-settings.min.js"></script>
<script src="cordova.js"></script>
Note: you don't have to use the complete ngCordova package. I suggest to create a Custom Build with file module.
Gives back an Object with all the stores values
Gives back the value of the specified key.
Gives back an Object with the values of the specified keys.
Removes the value of the specified key from settings.
Sets the value for the specified key. Value can be any JSON-like type: boolean, number, string, object, or array.
Where values = { key1: value1, key2: value2 ... }
Sets the values for the specified keys.
Returns an array with the stored keys in settings file.
Empties settings file.
Deletes settings file.
You can set the local filename for the settings file (default settings.txt).
angular.module('starter', ['ionic', 'ng-cordova-settings'])
.controller('mainCtrl', ['$scope', 'settings', '$ionicPlatform', function($scope, settings, $ionicPlatform) {
function testing1() {
settings.write('key0', 'value0').then(
function() {
testing2();
},
function(error) {
console.log(error);
}
);
}
function testing2() {
settings.write({ key1: 'value1', key2: 'value2' }).then(
function() {
testing3();
},
function(error) {
console.log(error);
}
);
}
function testing3() {
settings.read().then(function(result) {
console.log(result); // { key0: 'value0', key1: 'value1', key2: 'value2' }
});
settings.read('key1').then(function(result) {
console.log(result); // 'value1'
});
settings.read(['key0', 'key1']).then(function(result) {
console.log(result); // { key0: 'value0', key1: 'value1' }
});
settings.keys().then(function(result) {
console.log(result); // [ 'key0', 'key1', 'key2' ]
});
}
$ionicPlatform.ready(function() {
testing1();
});
}]);
ng-cordova-settings is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository.