无法读取未定义的属性

无法读取未定义的属性

我总是收到此错误:
 TypeError:无法读取未定义的属性“就绪”

这是我的代码:

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.directives','app.services', 'ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
});
});

angular.module('app.services', [])
.service('DatabaseService', [function($cordovaSQLite, $ionicPlatform) {
    var db;

    $ionicPlatform.ready(function () {
    if(window.cordova) {
        db = $cordovaSQLite.openDB("auftragDB");
    } else {
        db = window.openDatabase("auftragDB", "1.0", "Offline Artikel Datenbank", 10*1024*1024);
    }

    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS ArtikelTable (ticket_id number(10), kunde char(100))");
     });
}])


我真的不知道,为什么似乎找不到$ ionicPlatform ...

最好的祝福,
皮尔森

最佳答案

我认为您应该这样声明服务功能

angular.module('app.services', [])
.service('DatabaseService', ['$cordovaSQLite', '$ionicPlatform', function($cordovaSQLite, $ionicPlatform) {

 // When you pass second argument of .service() as array,
 // then the array should list all dependencies followed by function which use them

}])


要么

angular.module('app.services', [])
    .service('DatabaseService', function($cordovaSQLite, $ionicPlatform) {

     // or you can use a direct function with all dependencies as its parameter.
     // But dependencies injection will break if you do code minification

    })


服务可以具有自己的依赖性。就像在控制器中声明依赖项一样,您可以通过在服务的工厂函数签名中指定依赖项来声明依赖项。

资料来源:https://docs.angularjs.org/guide/services

更多:https://docs.angularjs.org/guide/di

关于javascript - Ionic:TypeError:无法读取未定义的属性“ready”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39088452/

10-12 15:43