Button not triggering in Application

Hi all,
I am newbie to ionic application. I’m using ionic V1.
I wish to create an simple application which interacts with sqlite db for details.
Initial page is a login page. Login button is not triggering.

Following is the code which i have tried so far;
index.html

Login.html


app.js

var db=null;
var example=angular.module(‘starter’, [‘ionic’, ‘ngCordova’]);

example.run(function($ionicPlatform,$cordovaSQLite) {
$ionicPlatform.ready(function() {
// if (cordova.platformId === “ios” && window.cordova && window.cordova.plugins.Keyboard) {
if (window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

  // Don't remove this line unless you know what you are doing. It stops the viewport
  // from snapping when text inputs are focused. Ionic handles this internally for
  // a much nicer keyboard experience.
  cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
db = $cordovaSQLite.openDB("Application.db");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS Login(id integer primary key, UserName text,Password text)");

});
})

example.controller(‘ExampleController’,function($scope,$cordovaSQLite,$state){
$scope.addInfo=function(){
var query = “INSERT INTO Login (UserName, Password) VALUES (?,?)”;
$cordovaSQLite.execute(db,query,[$scope.UserName,$scope.Password]);
}
})

The issue are

  1. login button click is not triggering from login.html, but it is triggered from index.html
  2. I have created db in app.js… Insert statement is not working, if it is in index.html.
  3. Is it possible to see dynamically created db. Please provide me default path of db?

Please provide me some reference to interact with db using ionic and angularjs

Thanks in Advance

You seem to have used ExampleController as your script for Login html. Have you included this in your index? also to answer your last question, I doubt you can see your database file. There is a parameter in the sqlite plugin where you can mention the location of the db ex: location: ‘default’.

Here is some reference for ionic-v1 sqlite.

Below is my code for app.js:

var mDb=null;
var IonicShowcase_App = angular.module('IonicShowcase', ['ionic', 'ngCordova'/*, 'ngMap'*/])

IonicShowcase_App.run(function($ionicPlatform,$cordovaSQLite,$ionicPopup ) {
    $ionicPlatform.ready(function() {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

            // Don't remove this line unless you know what you are doing. It stops the viewport
            // from snapping when text inputs are focused. Ionic handles this internally for
            // a much nicer keyboard experience.
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        mDb = window.sqlitePlugin.openDatabase({name: DB_NAME, location: 'default'});
        mDb.executeSql(QUERY_CREATE_TABLE, [], function(rs) {
              console.log('TABLE CREATED-' + rs.rows.item(0).mycount);
          }, function(error) {
            console.log('ERROR IN CREATING TABLE-' + error.message);
         });
         mDb.close(function () {
                 console.log("DB CLOSED");
             }, function (error) {
                 console.log("ERROR IN CLOSING DB-" + error.message);
             });

        if (window.Connection) {
            if (navigator.connection.type == Connection.NONE) {
                $ionicPopup.confirm({
                        title: "Internet Disconnected",
                        content: "The internet is disconnected on your device."
                    })
                    .then(function(result) {
                        if (!result) {
                            ionic.Platform.exitApp();
                        }
                    });
            }
        }

    });
})

Below is my code for SqliteController.js:

IonicShowcase_App.controller('SqliteController', function($scope,$ionicLoading,$cordovaToast) {

    $scope.formDetails={
        name:"",
        mobile:""
    };

    $scope.saveRequest=function(){
        $scope.showLoader();
        console.log($scope.formDetails);
        $scope.openDb();
        mDb.transaction(function(tx) {
            tx.executeSql('INSERT INTO UserRequests VALUES (?,?)', [$scope.formDetails.name,           $scope.formDetails.mobile]);
        }, function(error) {
            console.log('ERROR IN ADDING RECORD-' + error.message);
            $scope.dismissLoader();
            $scope.closeDb();
            $cordovaToast.showShortCenter(MSG_RECORD_SAVE_ERROR);
            $scope.clearAllFields();
        }, function() {
            console.log('RECORD ADDED');
            $scope.dismissLoader();
            $scope.closeDb();
            $cordovaToast.showShortCenter(MSG_RECORD_ADDED);
            $scope.clearAllFields();
        });
    };

    $scope.openDb=function(){
          mDb = window.sqlitePlugin.openDatabase({name: DB_NAME, location: 'default'});
    };

    $scope.closeDb=function(){
          mDb.close(function () {
                  console.log("DB CLOSED");
              }, function (error) {
                  console.log("ERROR IN CLOSING DB: " + error.message);
          });
    };

    $scope.showLoader=function(){
        $ionicLoading.show({
        template: '<ion-spinner icon="crescent" class="spinner-positive"></ion-spinner>',
        animation: 'fade-in',
        showBackdrop: true,
        });
    };

    $scope.dismissLoader=function(){
        $ionicLoading.hide();
    };

    $scope.clearAllFields=function(){
        $scope.formDetails.name="";
        $scope.formDetails.mobile=""
    };
})

Dont forget to include your controller in index.html

Thanks,
Santi Mendon

Thanks for your reply.

"You seem to have used ExampleController as your script for Login html. Have you included this in your index? "
But I have defined “ExampleController” inside app.js file

Please try the above mentioned insert query and let me know if you face any issues. Some console log in case of exceptions will be helpful.

Ok. Let me try out your code and let you know

Can you please post your html page?

var IonicShowcase_App = angular.module(‘IonicShowcase’, [‘ionic’, ‘ngCordova’/, ‘ngMap’/])
In the above statement you have mention IonicShowCase. Which part does it refers?

Usually it refers to starter

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport"
        content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title></title>

  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/ionic_showcase_style.css" rel="stylesheet">

  <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
  <link href="css/ionic.app.css" rel="stylesheet">
  -->

  <!-- ionic/angularjs js -->
  <script src="lib/ionic/js/ionic.bundle.js"></script>
  <!--<script src="http://maps.google.com/maps/api/js"></script>-->

  <!-- cordova script (this will be a 404 during development) -->
  <script src="lib/ng-cordova.min.js"></script>
  <script src="cordova.js"></script>
  <script src="lib/cryptojs-aes.js"></script>
  <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/sha256.js"></script>

  <!-- your app's js -->
  <script src="IonicShowcase/ionic_showcase_app.js"></script>
  <script src="IonicShowcase/ionic_showcase_route.js"></script>
  <script src="IonicShowcase/ionic_showcase_services.js"></script>
  <script src="IonicShowcase/controllers/LoginController.js"></script>
  <script src="IonicShowcase/controllers/DashboardController.js"></script>
  <script src="IonicShowcase/controllers/SqliteController.js"></script>
  <script src="constants/strings.js"></script>


</head>
<body ng-app="IonicShowcase">
<ion-nav-view>

</ion-nav-view>
</body>
</html>

The controller is referencing to the one mentioned in index.html ie IonicShowcase under body tag. So you can see here that your src consists of /ionic_showcase_app.js (ie app.js), SqliteController.js all defined inside the index.html.

Hi,
I have tried your code. But i am getting following errors.

I think, I might missed something.
Any ideas?

Can you please show me your app.js and your controller? Observe my app.js file. There is a variable IonicShowcase_App which is being referenced in the controllers.

app.js
var mDB=null;
var IonicShowcase_App=angular.module(‘IonicShowcase’, [‘ionic’,‘ngCordova’])

IonicShowcase_App.run(function($ionicPlatform,$cordovaSQLite,$ionicPopup) {
$ionicPlatform.ready(function() {

if (window.cordova && window.cordova.plugins.Keyboard) {

  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

  // Don't remove this line unless you know what you are doing. It stops the viewport
  // from snapping when text inputs are focused. Ionic handles this internally for
  // a much nicer keyboard experience.
  cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
mDB=window.sqlitePlugin.openDatabase({name:'ionicdb',location:'default'});
var QUERY_CREATE_TABLE='CREATE TABLE IF NOT EXISTS UserDetails (UserName text,Password text)';
mDB.executeSql(QUERY_CREATE_TABLE,[],function(rs){
  console.log('TABLE CREATED-' + rs.rows.item(0).mycount);
},function(error){
  console.log('ERROR IN CREATING TABLE-' + error.message);
});
mDB.close(function(){
  console.log("DB Closed");
},function(error){
  console.log("Error in closing db-"+error.message)
});

if(window.Connection){
  if(navigator.connection.type==connection.NONE){
    $ionicPopup.confirm({
      title: "Internet Disconnected",
      content: "The internet is disconnected on your device."
    })
    .then(function(result){
      if(!result){
        ionic.platform.exitApp();
      }
    });
  }
}

});
})

SqliteController.js
IonicShowcase_App.Controller(“SqliteController”,function($scope,$ionicLoading,$cordovaToast){
$scope.formdetails={
UserName:"",
Password:""
};
$scope.saveRequest=function(){
$scope.showLoader();
Console.log($scope.formdetails);
$scope.openDb();
mDB.transaction(function(tx){
tx.executeSql(‘INSERT INTO UserDetails VALUES (?,?)’, [$scope.formDetails.UserName,$scope.formDetails.Password]);
},function(error){
console.log(‘ERROR IN ADDING RECORD-’ + error.message);
$scope.dismissLoader();
$scope.closeDb();
$cordovaToast.showShortCenter(MSG_RECORD_SAVE_ERROR);
$scope.clearAllFields();
},function() {
console.log(‘RECORD ADDED’);
$scope.dismissLoader();
$scope.closeDb();
$cordovaToast.showShortCenter(MSG_RECORD_ADDED);
$scope.clearAllFields();
});
};
$scope.openDb=function(){
mDb = window.sqlitePlugin.openDatabase({name: ‘ionicdb’, location: ‘default’});
};

$scope.closeDb=function(){
      mDb.close(function () {
              console.log("DB CLOSED");
          }, function (error) {
              console.log("ERROR IN CLOSING DB: " + error.message);
      });
};

$scope.showLoader=function(){
    $ionicLoading.show({
    template: '<ion-spinner icon="crescent" class="spinner-positive"></ion-spinner>',
    animation: 'fade-in',
    showBackdrop: true,
    });
};

$scope.dismissLoader=function(){
    $ionicLoading.hide();
};

$scope.clearAllFields=function(){
    $scope.formDetails.UserName="";
    $scope.formDetails.Password=""
};

})

Went through your code in your repo. app.js and SqLiteController looks fine. Follow the below steps:

  1. Add an html page with a button. Onclick of the same call $scope.saveRequest().
  2. Your index.html does not include SqliteController. So follow what I have done in my index. You can use my index as it is.

Have shared my Git repo which contains a sample application. You can take a look at the implementation part. Code is in assets folder.

Thanks sir. Let me check out your repo

Went through your code in your repo. app.js and SqLiteController looks fine. Follow the below steps:

  1. Add an html page with a button. Onclick of the same call $scope.saveRequest().
  2. Your index.html does not include SqliteController. So follow what I have done in my index. You can use my index as it is.

I have tried this. still that error exists.