Search code examples
cordovapluginsionic2preferencesngcordova

Ionic 2 - Use installed Preferences Plugin from nGCordova


I am attempting to make my first Ionic app, and I need to create user preferences. To do this, the only resource I've found is cordova preferences, located here: http://ngcordova.com/docs/plugins/preferences/ The instructions to install this plugin are wonderfully simple, but once installed, I can not understand how I am supposed to implement this plugin in an Ionic 2 application. Can someone please provide me with an explanation on how to include ngCordova plugins in an Ionic 2 application and how to access their properties, or link me references you know of or github projects implementing this sort of thing. I've been trying to find this information for the past 2 days with no luck.

the example code on the plugin website is as follows:

 module.controller('MyCtrl', function($scope, $cordovaPreferences) {
 //Examples without dictionaries
 $scope.store = function() {
 $cordovaPreferences.store('key', 'myMagicValue')
  .success(function(value) {
    alert("Success: " + value);
  })
  .error(function(error) {
    alert("Error: " + error);
  })
 };

$scope.fetch = function() {
$cordovaPreferences.fetch('key')
  .success(function(value) {
    alert("Success: " + value);
  })
  .error(function(error) {
    alert("Error: " + error);
  })
};

But I've created several walkthrough projects and I've never encountered any code that looks like this, and I have no idea where to even begin with it.

I've looked around stackoverflow to try to find articles which might help with this issue, and while I have found similar questions, I've yet to see an answer that seems satisfactory/that I can comprehend.

Example: Ionic 2 Cordova and Typescript (App Preferences plugin)

This simply gives an alternate option by using SQLite, but I need to access the preferences section on the devices this app will be run on.

Thank you.


Solution

  • first of all ngCordova works over angular 1.X (so ionic 1.X), now there is another alternative to get this on ionic 2.X and angular 2.X, its called ionic-native.

    Ionic native docs

    "Ionic Native is a curated set of ES5/ES6/TypeScript wrappers for Cordova/PhoneGap plugins that make adding any native functionality you need to your Ionic, Cordova, or Web View mobile app easy."

    So just read the docs and follow them :)