$ cordovaFile未定义ngcordova插件

时间:2015-11-28 11:28:53

标签: android angularjs cordova plugins ngcordova

我有点问题。 我在visual studio 2015中启动了一个空的cordova应用程序,只是为了一些测试,我想在我的应用程序中尝试ng-cordova插件(无离子)。我遵循ng cordova文档,我在cordova.js之前和angular.js文件之后添加了ng-cordova.min.js文件。我没有添加任何东西到我的index.html文件和我写的cordova js文件后调用的脚本:

(function () {
    "use strict";

    document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

    function onDeviceReady() {
        
        
        var app_ng=angular.module('myApp', ['ngCordova']);
        console.log('device ready');

        $cordovaFile.getFreeDiskSpace()
       .then(function (success) {
           console.log(success);
       }, function (error) {
           console.log(error);
       });
    };


} )();
<html>
<head>
    <meta charset="utf-8" />

    <!--
        Customize the content security policy in the meta tag below as needed. Add 'unsafe-inline' to default-src to enable inline JavaScript.
        For details, see http://go.microsoft.com/fwlink/?LinkID=617521
    -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <title>ngcordovatest</title>

    <!-- ngcordovatest references -->
    <link href="css/index.css" rel="stylesheet" />
</head>
<body ng-app="myApp">
    <p>Hello, your application is ready!</p>
    <div id="console">

    </div>

    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="scripts/jquery.js"></script>
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/ng-cordova.min.js"></script>
    <script src="scripts/ng-cordova-mocks.min.js"></script>
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    
    <script src="scripts/index.js"></script>
</body>
</html>

我总是收到以下错误:

未捕获的ReferenceError:$ cordovaFile未定义

我使用Android设备进行USB连接调试。

有什么问题?我无法弄清楚。谢谢。

1 个答案:

答案 0 :(得分:1)

因为'angular'只是当前范围内的有用对象。所以,试试像

这样的东西
        function onDeviceReady() {
    .........
    angular.element(document).ready(function () {
            angular.element(document.body).injector().invoke(['$cordovaFile', function($cordovaFile){
             $cordovaFile.getFreeDiskSpace()
                   .then(function (success) {
                       console.log(success);
                   }, function (error) {
                       console.log(error);
                   });
            }]);
        }
});