CordovaApp-AngularJs-app.initialize()->undefined不是函数

CordovaApp - AngularJs - app.initialize() -> undefined is not a function

本文关键字:undefined 函数 gt initialize CordovaApp-AngularJs-app      更新时间:2023-11-21

我有一个适用于android的混合体,由cordova+onsen+angularJs制成。

这是index.html文件:

<html>
<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"/>
</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>

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

<script src="lib/onsen/js/angular/angular.js"></script>   
<script src="lib/onsen/js/angular/angular-touch.js"></script>    
<script src="lib/onsen/js/onsenui.min.js"></script>  
  <script src="http://maps.google.com/maps/api/js?sensor=false"></script>         
  <script src="js/lodash_3.2.0.min.js"></script>  
  <script src="js/angular-google-maps_2.0.12.min.js"></script>
    <script src="js/jquery.min.js"></script>   
<script src="js/plugins.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>    
<script type="text/javascript" src="cordova.js"></script>
  <script>
    app.initialize();
   </script>
  </body>
 </html>

这是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);    

})();

但是chrome检查控制台说,undefined不是一个函数,引用了index.html 中的脚本

   <script>app.initialize();</script>

怎么了?感谢

自己找到解决方案:

我创建了一个名为index.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);
}
 };

  app.initialize(); //moved here this instruction

然后在index.html正文中,作为最后导入的脚本:

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

一切正常。