简体   繁体   中英

Onsen - Cordova - AngularJs module is not available

I've an hybrid app for android, i'm using cordova and onsen + angular js.

This is my index.html file:

 <html lang="en"  ng-app="AppModel">
  <head>
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <title>AppModel</title>  

    <!-- <link rel="stylesheet" href="css/plugins.css"/> -->
    <link rel="stylesheet/less" href="css/plugins.less"/>

    <link rel="stylesheet" href="lib/onsen/css/onsenui.css">  
    <link rel="stylesheet" href="lib/onsen/css/onsen-css-components.css">  

    <link rel="stylesheet/less" href="css/app.less"/>
    <link rel="stylesheet/less" href="css/base-layout.less"/>

    <script src="css/less.min.js" type="text/javascript"></script>


    <script src="http://maps.google.com/maps/api/js"></script>         
    <script src="js/lodash_3.2.0.min.js"></script>  


    <script src="lib/onsen/js/angular/angular.js"></script>   

    <script src="js/angular-google-maps_2.0.12.min.js"></script>
    <script src="lib/onsen/js/angular/angular-touch.js"></script> 

    <script src="lib/onsen/js/onsenui.min.js"></script>     


    <script src="js/jquery.min.js"></script>   
    <script src="js/plugins.js"></script> 

    <script type="text/javascript" src="cordova.js"></script>

    <script src="js/appStart.js"></script>      
    <script src="js/data/data.js"></script>    
    <script src="js/angularApp/controllers.js"></script>      
    <script src="js/angularApp/directives.js"></script>      
    <script src="js/angularApp/filters.js"></script>    


</head>

<body >    

<ons-sliding-menu
    menu-page="modules/core/menu.html" main-page="modules/account_profile/login.html" side="left"
    var="menu" type="reveal" max-slide-distance="260px" swipable="true" swipe-target-width="50">
</ons-sliding-menu>

 </body>
 </html>

This is the appStart.js

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'

exitFunction : function(){
     if (navigator.notification.confirm("Vuoi chiudere l'app?", 
            function(index) {
                if (index == 1) { // OK button
                    navigator.app.exitApp(); // Close the app
                }
            },
            "AppModel",
            ["Ok","Annulla"]
        ));
},

onDeviceReady: function() {
    app.receivedEvent('deviceready');

    ons.setDefaultDeviceBackButtonListener(function() {
        if (navigator.notification.confirm("Vuoi chiudere l'app?", 
            function(index) {
                if (index == 1) { // OK button
                    navigator.app.exitApp(); // Close the app
                }
            },
            "AppModel",
            ["Ok","Annulla"]
        ));
    });

    /*
    // Open any external link with InAppBrowser Plugin
    $(document).on('click', 'a[href^=http], a[href^=https]', function(e){

        e.preventDefault();
        var $this = $(this); 
        var target = $this.data('inAppBrowser') || '_blank';

        window.open($this.attr('href'), target);

    });
    */

    $(document).on('click', 'a', function(e){

        e.preventDefault();
        var $this = $(this); 
        //var target = $this.data('inAppBrowser') || '_blank';

        window.open($this.attr('href'), "_system");

    });


},
// Update DOM on a Received Event
receivedEvent: function(id) {
    //var parentElement = document.getElementById(id);
    //var listeningElement = parentElement.querySelector('.listening');
    //var receivedElement = parentElement.querySelector('.received');

    //listeningElement.setAttribute('style', 'display:none;');
    //receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}

};

 (function() {

var app = angular.module('AppModel', ['onsen', 'angular-carousel', 'uiGmapgoogle-maps'])    
.config( [
    '$compileProvider',
    function( $compileProvider )
    {   
        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|ftp|mailto|tel|geo):/);            
    }
]);

document.addEventListener('deviceready', function() {
    angular.bootstrap(document, ['AppModel']);       
}, false);

 })

And data.js, controller.js, filter.js, directive.js are all like this:

 var app = angular.module('AppModel'); //this is the first line in each file

 app.factory('MenuData', function(){ [...]

But when i launch the app in google chrome , the console says:

Uncaught Error: [$injector:nomod] Module 'AppModel' 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.2.10/ $injector/nomod?p0=AppModel

This message appears for the files data.js, controller.js, filter.js, directives.js due to the first line of each file.

I don't know what to do. Can someone help me?

Doesn't look like you're executing the line where the app module is created.

You must run the line

angular.module('appName', [dependencies]);

before you created your controllers and services, like the error message says.

Are you calling the anonymous function that creates the module? Just add an empty parenthesis and it should run:

(function() {
  var app = angular.module(...
})();

As the error message says, you must specify the dependencies as the second argument. If none is needed, then pass an empty array.

change

var app = angular.module('AppModel'); //this is the first line in each file

to

var app = angular.module('AppModel', []); //this is the first line in each file

and run it again.

Additional information: https://docs.angularjs.org/api/ng/function/angular.module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM