2015-07-21 50 views
2

今天我走到了瘋狂的邊緣。AngularJs/ngCordova自定義構建離子注入模塊錯誤

我的目標僅僅是包含一個插件ngCordova(在本例中爲:cordova-plugin-device)。和一點點工作。

我的出發點是一個項目離子選項卡,沒有更多。

根據我收到的建議,我首先進入網站ngCordova,然後構建一個自定義ng cordova.js,其中只包含我需要的設備。

我整合到您的項目位置:/www/lib/ngCordova/dist/ng-cordova.js。

我改變我的index.html如下:

... 
<!-- ionic/angularjs js --> 
<script src="lib/ionic/js/ionic.bundle.js"></script> 

<script src="lib/ngCordova/dist/ng-cordova.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>... 

我在我的應用程序模塊提出了依賴注入是這樣的:

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

最後,我修改控制器「DashCtrl」來整合插件:

.controller('DashCtrl', function($ionicPlatform, $scope, $cordovaDevice) { 
    $ionicPlatform.ready(function() { 
    $scope.$apply(function() { 
      // sometimes binding does not work! :/ 

      // getting device infor from $cordovaDevice 
      var device = $cordovaDevice.getDevice(); 

      $scope.manufacturer = device.manufacturer; 
      $scope.model = device.model; 
      $scope.platform = device.platform; 
      $scope.uuid = device.uuid; 

      }); 

    }); 
}) 

而我在我的瀏覽器(在波紋下)得到這個錯誤:

Uncaught Error: [$ injector: modulerr] Failed to instantiate starter unit due to: 
Error: [$ injector: modulerr] Failed to instantiate ngCordova Module due to: 
Error: [$ injector: modulerr] Failed to instantiate ngCordova.plugins Module due to: 
Error: [$ injector: modulerr] Failed to instantiate the module device due to: 
Error: [$ injector: nomod] Module 'device' is not available! Either you misspelled the name or it forgot to load module. If a unit Registering assurer That You Specify the dependencies as the second argument. 

但奇怪的是,即使沒有在我的代碼碰到任何東西,如果我做了:

bower install ngCordova --save 

它下載完整版ngCordova,並在那裏,一切都完美的作品。

我真的不明白我的錯誤在哪裏。我希望你們中的某個人能幫助我理解。

非常感謝您花時間閱讀我的留言以及您花時間回答(並對不起我的英文故障)。

回答

3

這是構建系統中的一個錯誤。

打開ngCordova.js文件,並更改

angular.module('ngCordova.plugins', [ 
     'device' 
    ]); 

angular.module('ngCordova.plugins', [ 
     'ngCordova.plugins.device' 
    ]); 
0

有你包括由插件:cordova plugin add cordova-plugin-device

你可以嘗試使用完整的ng-cordova.js來檢查嗎?

更重要的是,不要在控制器的$ scope上編寫代碼。 使用「controller as <alias>」來定義別名,並像這樣註冊你的var:`。

controller('DashCtrl', function($ionicPlatform, $scope, $cordovaDevice) { 
var self = this; 
    $ionicPlatform.ready(function() { 
      // sometimes binding does not work! :/ 

      // getting device infor from $cordovaDevice 
      var device = $cordovaDevice.getDevice(); 

      self.manufacturer = device.manufacturer; 
      self.model = device.model; 
      self.platform = device.platform; 
      self.uuid = device.uuid; 


    }); 
})` 

<alias>.myVarOrFucntion

使用它的HTML