TypeError:无法读取属性'插件' of cordovaLocalNotification插件的未定义

时间:2016-10-09 13:14:19

标签: javascript angularjs cordova ionic-framework cordova-plugins

我正在使用离子平台开发混合应用程序。我正在实施cordovalocalnotification功能,但它提示无法读取属性'插件'未定义的。以下是我的代码。任何人都可以帮我解决这个问题。

的index.html

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
  <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="lib/ngCordova/dist/ng-cordova.min.js"></script>
  <script src="cordova.js"></script>

  <!-- your app's js -->
  <script src="js/app.js"></script>
</head>

<body ng-app="starter" ng-controller="SampleController">

  <ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Local Notification Sample</h1>
    </ion-header-bar>
    <ion-content>

        <button class="button button-block button-positive" ng-click="scheduleInstantNotification()">
          Instant 
        </button>

    </ion-content>
  </ion-pane>
</body>

</html>

app.js

angular.module('starter', ['ionic', 'ngCordova'])
  .run(function ($ionicPlatform, $rootScope) {
    $ionicPlatform.ready(function () {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        $rootScope.$on('$cordovaLocalNotification:schedule',
            function (event, notification, state) {
                console.log("SCHEDULE");
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:trigger',
            function (event, notification, state) {
                console.log("TRIGGER");
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:update',
            function (event, notification, state) {
                console.log('UPDATE');
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:cancel',
            function (event, notification, state) {
                console.log('CANCEL');
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });
    });
  })
  .controller('SampleController',
    function ($scope, $cordovaLocalNotification, $ionicPlatform) {
        $ionicPlatform.ready(function () {

            $scope.scheduleInstantNotification = function () {
                $cordovaLocalNotification.schedule({
                    id: 1,
                    text: 'Instant Notification',
                    title: 'Instant'
                }).then(function () {
                    alert("Instant Notification set");
                });;
            };
            };
        });
    });

2 个答案:

答案 0 :(得分:1)

使用window.cordova.plugins代替cordova.plugins

if (window.cordova && window.cordova.plugins.Keyboard) {
      //cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      window.cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}

答案 1 :(得分:0)

使用cordova插件add ionic-plugin-keyboard

添加插件

将以上代码写在ondeviceready上,它应该正常工作。

您可以使用开发人员工具进行验证。调试您的移动应用程序并检查cordova.plugins.Keyboard