$ cordovaSQLite.execute不是一个函数

时间:2017-03-14 08:34:01

标签: angularjs sqlite ionic-framework

我是离子1的新手。我想通过表单将数据插入到我的sqlite表中。但是当我点击提交按钮时,它会显示以下错误:

TypeError: $cordovaSQLite.execute is not a function
    at ChildScope.$scope.insertNewItem (addnewitemCtrl.js:12)
    at fn (eval at compile (ionic.bundle.js:27643), <anonymous>:4:230)
    at ionic.bundle.js:65429
    at ChildScope.$eval (ionic.bundle.js:30400)
    at ChildScope.$apply (ionic.bundle.js:30500)
    at HTMLInputElement.<anonymous> (ionic.bundle.js:65428)
    at defaultHandlerWrapper (ionic.bundle.js:16792)
    at HTMLInputElement.eventHandler (ionic.bundle.js:16780)
    at triggerMouseEvent (ionic.bundle.js:2953)
    at tapClick (ionic.bundle.js:2942)

我有一个不同的控制器页面。这是我的控制器代码:

angular.module('app.addNewItemCtrl', ['ngCordova'])

.controller('addNewItemCtrl', ['$scope', '$stateParams','$state','$cordovaSQLite','itemFormData','$ionicPlatform', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,$state,itemFormData,$cordovaSQLite,$ionicPlatform) {

            $ionicPlatform.ready(function () {
              $scope.item={};
        $scope.insertNewItem = function(){
            var query="INSERT into items(itemname) VALUES(?)";
            $cordovaSQLite.execute(db,query,[$scope.item.itemname]).then(function(result){
                console.log(result.rows.length);
            },function(error){
                console.log(error);

            }); 

        }
    });



}])

在我的app.js中,我创建了数据库和表格;这是我的应用js代码段:

if (window.cordova) {
      db = $cordovaSQLite.openDB({ name: "pos.db" }); //device
     console.log("not in browser");
    }else{
      db = window.openDatabase("pos.db", '1', 'my', 1024 * 1024 * 100); // browser
      console.log("browser");

    }

    $cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS items(id integer primary key,itemname text)");

我只想将项目名称添加到数据库中。这是我的表单模板代码段:

  <form id="addNewItem-form2" class="list" ng-submit="submitForm(item)">
      <label class="item item-input item-floating-label" id="addNewItem-input4">
        <span class="input-label" ">Item name</span>
        <input type="text" ng-model="item.itemname" placeholder="enter item name">

       <input type="submit" ng-click="insertNewItem()" class="button button-block button-positive" value="Submit">
    </form>

1 个答案:

答案 0 :(得分:0)

依赖注入注释和函数参数的顺序不匹配。

在您的代码中,$cordovaSQLite实际上是itemFormData,它没有执行方法。

确保所有项目以相同的顺序显示:

.controller('addNewItemCtrl', ['$scope', '$stateParams','$state','$cordovaSQLite','itemFormData','$ionicPlatform', 
  function ($scope, $stateParams,$state,$cordovaSQLite,itemFormData, $ionicPlatform) {
    // $cordovaSQLite is now the expected object 
}])