未捕获的错误:cordovaHTTP不可用

时间:2015-11-15 09:19:45

标签: ios angularjs cordova ionic phonegap-plugins

我想将cordovaHTTP插件用于后台线程,我是从github链接(https://github.com/wymsee/cordova-HTTP)安装的。

但是当我将它注入我的应用程序时,它会给我一个错误。

Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module cordovaHTTP due to:
Error: [$injector:nomod] Module 'cordovaHTTP' 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.
http://errors.angularjs.org/1.3.13/$injector/nomod?p0=cordovaHTTP

我的app.js代码是: -

angular.module('starter', ['ionic', 'ngCordova','cordovaHTTP','starter.controllers', 'ngStorage', 'toaster','angular.filter','angular-loading-bar','ngTagsInput','ngIOS9UIWebViewPatch'])

我的Article.js文件代码是: -

Article.prototype.articleDetails = function(articles,createdDate) {
                      var access_token = $localStorage.accessToken;
                    if(createdDate) {
                       articles.forEach(function(bookmark) {
                           if(bookmark.created_at > createdDate) {
                            if(bookmark.mode != 'youtube' && bookmark.mode != 'pdf' ) {
                               cordovaHTTP.post("https://www.example.com/get_article_detail?access_token="+access_token, {
                                    bookmark_file: bookmark.bookmark_file,
                                },
                                {}, // optional headers
                                function(msg) {alert("OK: " + msg)},
                                function(msg) {alert("ERROR: " + msg)}
);

                            }
                                 }
            }); 
                    } else {
                    localstorage.set('isUserTraverseColony', 1);
            articles.forEach(function(bookmark) {
                            if(bookmark.mode != 'youtube' && bookmark.mode != 'pdf' ) {
                                cordovaHTTP.post("https://www.example.com/get_article_detail?access_token="+access_token, {
                                    bookmark_file: bookmark.bookmark_file,
                                },
                                {}, // optional headers
                                function(msg) {alert("OK: " + msg)},
                                function(msg) {alert("ERROR: " + msg)}
                                );



                            }
            });
                    }
        }

我的index.html文件是: -

<!DOCTYPE html>
<html>
  <head>
    <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/loading-bar.css" rel="stylesheet">
    <link href="lib/angularjs-toaster/toaster.min.css" rel="stylesheet">
    <link rel="stylesheet" href="lib/ngtoast/dist/ngToast.min.css">
    <link href="css/style.css" rel="stylesheet">
 <link href="lib/ng-tags-input/ng-tags-input.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>
    <script src="lib/angularjs-toaster/toaster.min.js"></script>
    <script src="lib/ngtoast/dist/ngToast.min.js"></script>
    <script src="lib/angular-filter/dist/angular-filter.min.js"></script>
    <script src="lib/ng-tags-input/ng-tags-input.min.js"></script>
           <script src="lib/angular-ios9-uiwebview.patch.js"></script>
    <script src="js/ng-cordova.min.js"></script>
    <script src="js/ngStorage.js"></script>
    <script src="js/ng-cordova-oauth.min.js"></script>
    <script src="js/loading-bar.js"></script>
   <!-- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js"></script>
     cordova script (this will be a 404 during development) -->
     <script src="cordova.js"></script>
     <script src="js/app.js"></script>
    <!-- your app's js -->
    <script src="js/controllers.js"></script>
    <script src="js/intro.js"></script>
    <script src="js/usersCtrl.js"></script>
    <script src="js/userFactory.js"></script>
    <script src="js/categoriesCtrl.js"></script>
    <script src="js/categoriesFactory.js"></script>
     <script src="js/homeCtrl.js"></script>
    <script src="js/articlesCtrl.js"></script>
    <script src ="js/articlesFactory.js"></script>
    <script src="js/readerCtrl.js"></script>
    <script src="js/common.js"></script>
    <script src="js/favouriteCtrl.js"></script>
    <script src="js/readerFactory.js"></script>
    <script src="js/dbService.js"></script>
     <script src="js/errorFactory.js"></script>
     <script src="js/localStorage.js"></script>
     <script src ="js/favouriteFactory.js"></script>
    <script src ="js/cordovaHTTP.js"></script>
  </head>
  <body ng-app="starter">
   <div class="articleLoaded" ng-if="isLoadArticle == 0">
   <div class="loading-inner">
                <img ng-src="img/loading.gif">
          <span>Retrieving...</span>

          </div>
      </div>

       <div class="articleLoaded" ng-if="isLoadLogin == 0">
   <div class="loading-inner">
                <img ng-src="img/loading.gif">
          <span>Login...</span>

          </div>
      </div>

<ion-nav-view animation="slide-left-right" class="slide-left-right">
    </ion-nav-view>
     <div class="toastMessage" ng-if="$root.toastMessage"><span>No network connection!  Please check your network.</span></div>
  <toast></toast>
  </body>
</html>

任何想法?

感谢。

2 个答案:

答案 0 :(得分:0)

看看我的这个插件的fork,你不需要在安装插件之前直接注入依赖,只需调用这个:

https://github.com/gergom/cordova-HTTP.git

Ionic(AngularJS)中的示例

angular.module('start.services').factory('AuthenticationService', function(SERVER, $q, $localstorage) {

function login(email, password){
    var url = SERVER.urlApi + '/login';
    var headers = { 'X-Authorization-API': SERVER.code };
    var parameters = { email: email, password: password };
    var deferred = $q.defer();
    var promise = deferred.promise;
    window.cordovaHTTP.post(url, parameters, headers, function(response) {            
        deferred.resolve(response);
    }, function(error) {
        deferred.reject(error);
    });
    return promise;
}

return {
    doLogin: login
}
});

这适用于Android 6.0.1和IOS 9.3.3  我希望我能帮到你。

答案 1 :(得分:0)

来自另一个帖子的my answer。希望这有帮助。

  1. 确保在index.html中的cordova js文件后加载角度js文件。
  2. 这是我的index.html:

    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/jquery/jquery-3.2.1.min.js"></script>
    <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>
    
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/routes.js"></script>
    <script src="js/directives.js"></script>
    <script src="js/services.js"></script>
    
    1. 是否定义了angular
    2.   

      如果定义了角度,则cordova-HTTP将仅实例化cordovaHTTP模块,否则它将回退到window.cordovaHTTP。

      您可以使用此:

      document.addEventListener("deviceready", function () {
        console.log(window.cordovaHTTP);
        console.log(window.CordovaHttpPlugin);  // For some version it's instantiated in this name if angular is not defined.
      }, false);
      
      1. 手动引导您的AngularJS应用,而不是使用ng-app属性。
      2. 代码:

        <script>
          angular.element(document).ready(function() {
              document.addEventListener("deviceready", function () {
                  console.log(window.cordovaHTTP);
                  console.log(window.CordovaHttpPlugin);  // For some version it's instantiated in this name if angular is not defined.
                  angular.bootstrap(document, ['MyAppName']);
              }, false);
          });
        </script>
        
        1. 在控制器或服务中使用时,无需添加$
        2. 就这样使用它:

          angular.module('MyAppName.services', [])
          .service('MyService', ['cordovaHTTP', function(cordovaHTTP){
              // Your codes are here.
          }]);