angularjs - Angular 模块 'cordovaHTTP' 不可用

标签 angularjs cordova http ionic-framework

我正在运行 Ionic 并尝试使用 cordovaHTTP 进行 SSL 固定。我按照 github 上的说明进行操作但我收到模块不存在的错误消息。

我的index.html

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>

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

我的 app.js:

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

在 services.js 中:

angular.module('starter.services', [])

.service('MyService', function(cordovaHTTP) {

    cordovaHTTP.enableSSLPinning(true, function() {
        console.log('successful ssl pin');

        cordovaHTTP.useBasicAuth("user", "pass", function() {
            console.log('successful basic auth!');

            cordovaHTTP.get(url, function(response) {
                console.log(response.status);
            }, function(response) {
                console.error(response.error);
            });
        }, function() {
            console.log('error basic auth');
        });     
    }, function(){
        console.log('error ssl pin');
    });
});

这会导致错误:

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.

我试过无数次搞砸这个,但没有成功。感谢您的帮助。

最佳答案

实际上,我认为您正在多次初始化您的模块。在 Angular 中,factory/services 是单例的。你的代码看起来像这样

angular.module('starter', ['ionic', 'MyService', 'cordovaHTTP']) //setters for a module

请确保您输入的是正确的服务或工厂名称。你也应该改变你的服务声明方式,如下所示

angular.module('starter') //Don't use [] it is only getter of above initialized module

.service('MyService', function(cordovaHTTP){ 
      //Code
});

另外请不要忘记最后加载app.js。希望对您有所帮助。

关于angularjs - Angular 模块 'cordovaHTTP' 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841537/

相关文章:

xcode - 为什么 `ionic run ios`出现 "No matching provisioning profile found"错误?

python - 编写 Python 音乐流媒体

javascript - 在错误超时之前重新发送 HTTP post

angularjs - Angular $ watch空值错误

javascript - angularjs 使用 fileselect 连接下拉列表

angularjs - 如何在 AngularJS 中以不同方式显示模型和 View

android - Phonegap Android 内联 SVG

javascript - cordova 平台在 Windows 8 上添加 android 错误

ruby-on-rails - 我们如何使用 AJAX 发送/获取 http header 信息?

angularjs - $ route.reload()不适用于ui-router