ionicframework CRUD operation using SQLite



我添加了创建此示例应用程序所需的ngcordova SQLite插件

索引.html

<!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/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>
    <!-- cordova script (this will be a 404 during development) -->
    <script src="js/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Crud & SQLite</h1>
      </ion-header-bar>
      <ion-content ng-controller="AccountController">
          <form ng-submit="addAccount()">
            <div class="list">
              <label class="item item-input item-stacked-label">
                <span class="input-label">First Name</span>
                <input type="text" placeholder="John" ng-model="firstnameText">
              </label>
              <label class="item item-input item-stacked-label">
                <span class="input-label">Last Name</span>
                <input type="text" placeholder="Suhr" ng-model="lastnameText">
              </label>
              <div class="padding">
                <button class="button button-block button-positive">Create Account</button>
              </div>
            </div>
          </form>
          <ul class="list list-inset">
            <li class="item item-divider">
              {{accounts.length}} records
            </li>
            <li class="item" ng-repeat="account in accounts">
              <i class="icon ion-person"></i>&nbsp; - &nbsp;
              <span>{{account.firstname}} {{account.lastname}}</span>
            </li>
          </ul>
      </ion-content>
    </ion-pane>
  </body>
</html>

应用.js

var db = null;
angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
    db = $cordovaSQLite.openDB({ name: "my.db" });
    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXIST people (id integer primary key, firstname text, lastname text)");
  });
})
.controller('AccountController', function($scope, $cordovaSQLite) {
  $scope.accounts = function() {
    var query = "SELECT firstname, lastname FROM people";
    $cordovaSQLite.execute(db, query);
  }
  $scope.addAccount = function(){
    var query = "INSERT INTO people (firstname, lastname) VALUES (?, ?)";
    $cordovaSQLite.execute(db, query, [$scope.firstnameText, $scope.lastnameText]);
    $scope.firstnameText = '';
    $scope.lastnameText = '';
  }
});

我已经在我的设备上运行了我的应用程序,并且列表中没有添加任何内容,这意味着我没有将任何内容保存到数据库中。请帮忙吗?谢谢

我遇到了这个问题 - 经过一些研究,我通过在加载 Angular 之前等待科尔多瓦的deviceready事件来解决它。查看 API 文档,了解如何进行手动 Angular 初始化

基本上,您需要删除ng-app指令,并在 Cordova 的 deviceready 事件触发后对之前所在的元素调用 angular.bootstrap

我像这样添加了一个 delayedAngular.js 文件(不要忘记将其作为索引中的<script>添加.html

angular.element(document).ready(function() {
  console.log("BOOTSTRAPPING...");
  if (window.cordova) {
    document.addEventListener('deviceready', function() {
      console.log("window.cordova detected");
      angular.bootstrap(document.body, ['myCoolApp']);
    }, false);
  } else {
    console.log("window.cordova NOT detected");
    angular.bootstrap(document.body, ['myCoolApp']);
  }
});

在上面的代码中,将myCoolApp替换为主应用模块的名称。我将尝试找到我找到此博客文章以获得应有的信用。

我发现让它回退到 WebSQL 数据库在浏览器中进行测试也非常有帮助,因为在设备上进行 SQLite 调试是一种痛苦。我在我的应用程序中使用了下面的代码 - 它使用 Angular 承诺,因此请确保您熟悉它们(如果您需要警报,请确保也注入$window。我没有扎根我的手机,所以无法直接检查设备上的SQLite数据库:-/)

var initDB = function(dbName){
  $log.log("Opening DB...");
  var q = $q.defer();
  var db;
  if($cordovaSQLite && $window.sqlitePlugin !== undefined){
    $window.alert("SQLite plugin detected");
    db = $cordovaSQLite.openDB({ name: dbName });
    q.resolve(db);
  }
  else {
    db = $window.openDatabase(
      dbName,
      "0.0.1",
      "My DB",
      200000,
      function(){
        $window.alert("Created WebSQL DB!");
      }
    );
    q.resolve(db);
  }
  return q.promise;
};

最新更新