【问题标题】:Cordova Camera and Ionic Framework - Controller and Index.html not connectingCordova 相机和 Ionic 框架 - 控制器和 Index.html 未连接
【发布时间】:2015-10-27 19:57:56
【问题描述】:

我正在尝试使用 Ionic Framework 和 Cordova 实现一个相机应用程序,并努力理解为什么它会抛出以下错误并因此无法实例化相机:

      0     444468   error    Uncaught SyntaxError: Unexpected token ., http://192.168.0.10:8100/js/app.js, Line: 9
      1     444540   error    Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
      Error: [$injector:nomod] Module 'starter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

不幸的是,以下问题和答案没有帮助我回答我的问题:

Ionic Framework / Cordova Camera

Cordova Camera - Ionic

navigator.camera.getPicture doesn’t call success callback - ionic framework - cordova

我参考了以下教程来完成这项工作,但没有成功(有时我会更加困惑):

https://www.youtube.com/watch?v=1KItyjeqhx0

https://github.com/driftyco/ionic-example-cordova-camera

http://learn.ionicframework.com/formulas/cordova-camera/

index.html

  <!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
    <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="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-assertive">
    <h1 class="title">Title of App</h1>
  </ion-header-bar>
  <ion-content ng-controller="CameraCtrl">
    <div class="list card"
    <div class="item item-image">
      <img ng-src="{{pictureUrl}}">
    </div>
  </div>
  <div class="padding">
    <button ng-click="takePicture()"
    class="button button-block button-assertive">Take a Photo</button>
  </div>
  </ion-content>
</ion-pane>
 </body>
 </html>

app.js

    // Ionic Starter App

    angular.module('starter', ['ionic', 'ngCordova']);

  .controller('CameraCtrl', function($scope, $cordovaCamera) {

   $scope.pictureUrl = 'http://placehold.it/300x300';

   $scope.takePicture = function() {
   $cordovaCamera.getPicture({})
      .then(function(data) {
       console.log('camera data': + angular.toJson(data));
      }, function(error) {
        console.error('camera error': + angular.toJson(data));
      });
    }

  )};

  .run(function($ionicPlatform) {
   $ionicPlatform.ready(function() {
     if(window.cordova && window.cordova.plugins.Keyboard) {
       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
     }
     if(window.StatusBar) {
       StatusBar.styleDefault();
     } 
   });
 })

提前感谢您的所有帮助!

【问题讨论】:

    标签: cordova camera ionic-framework ionic cordova-plugins


    【解决方案1】:

    Jad Salhani 是对的。

    我会在这里回答,即使这应该是评论,因为我没有足够的空间。

    这里有 2 个选项。您可以定义一个要在模块 (app.js) 中使用的变量:

    var app = angular.module('starter', ['ionic', 'ngCordova']);
    

    并使用 app 变量:

    app.run(function($ionicPlatform) {
       $ionicPlatform.ready(function() {
         if(window.cordova && window.cordova.plugins.Keyboard) {
           cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
         }
         if(window.StatusBar) {
           StatusBar.styleDefault();
         } 
       });
     });
    
    app.controller('CameraCtrl', function($scope, $cordovaCamera) {
       ...
    });
    

    或删除半列:

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

    【讨论】:

    • 100% 正确,我错过了 .controller 声明末尾的分号,这就是显示另一个错误的原因!很好的说明
    • @JadSalhani:是的,我的只是一个澄清。不过,你的答案是好的:-)
    【解决方案2】:
       angular.module('starter', ['ionic', 'ngCordova']);
    

    这是你的错误所在,那里不应该有分号。

    它的angular.module('moduleName').controller('controllerName', function(){});

    【讨论】:

    • 尽管您提出了修复建议,但现在出现以下错误0 830977 error Uncaught SyntaxError: Unexpected token :, http://192.168.0.10:8100/js/app.js, Line: 16 1 831058 error Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to: Error: [$injector:nomod] Module 'starter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    • 你让我修复的代码现在看起来像这样angular.module('starter' ['ionic', 'ngCordova']).controller('CameraCtrl', function($scope, $cordovaCamera)
    • @stefbmt 检查下面的答案,我错过了 .controller 声明末尾的分号,这就是您仍然收到此错误的原因。给您添麻烦了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多