"设备未定义"在我的离子应用程序

时间:2017-02-06 10:55:19

标签: javascript angularjs ionic-framework cordova-plugins

我知道这是多次询问,但我找不到解决方案。我有一个离子应用程序,我想获得设备UUID。这是我的代码: 这是我的index.html;

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/angular-animate/angular-animate.min.js"></script>
<script src="bower_components/angular-touch/angular-touch.min.js"></script>
<script src="lib/angular-mocks/angular-mocks.js"></script>
<script src="bower_components/moment/min/moment.min.js"></script>
<script src="bower_components/angular-moment/angular-moment.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/1.10.3/TweenMax.min.js"></script>
<script src="lib/bootstrap-sweetalert/dist/sweetalert.min.js"></script>

<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>

我的控制器;

.controller('AppCtrl', function($scope, $rootScope, $state, $ionicPlatform, $cordovaDevice) {

$scope.uuid = "";
$ionicPlatform.ready(function() {
  if (ionic.Platform.isAndroid()) {
    $scope.uuid = $cordovaDevice.getUUID();
  } else {
    console.log('It is not an android');
  };

});

})

当我运行代码控制台时,给我

  

未捕获的ReferenceError:未定义设备

我也将ngCordova导入app.js模块。我做的一切都正确,但我仍然无法处理它。谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:1)

尝试在索引页面上更改脚本的导入顺序。

<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<script src="lib/ionic/js/ionic.bundle.js"></script>

还请使用命令?

检查您是否安装了cordovaDevice插件
cordova plugin add org.apache.cordova.device

答案 1 :(得分:0)

问题是基于index.html中列出的.js文件。我不知道为什么,但是当我将<script src="cordova.js"></script>导入index.html时就可以了。但是当从另一个文件(例如<script src="js/cordova.js"></script>)调用cordova.js时,它不起作用。所以,我改变了我的index.html;

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/angular-animate/angular-animate.min.js"></script>
<script src="bower_components/angular-touch/angular-touch.min.js"></script>
<script src="lib/angular-mocks/angular-mocks.js"></script>
<script src="bower_components/moment/min/moment.min.js"></script>
<script src="bower_components/angular-moment/angular-moment.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/1.10.3/TweenMax.min.js"></script>
<script src="lib/bootstrap-sweetalert/dist/sweetalert.min.js"></script>

<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>

没关系。