AngularJS,Onsen UI,Phonegap,alert在控制器内显示两次

时间:2014-09-24 07:28:49

标签: javascript android angularjs cordova onsen-ui

我正在编写一个结合OnsenUI的AngulasJS应用程序,并使用phonegab将其包装到Android的应用程序容器中。

当我的应用程序作为新的应用程序进程运行时,问题就开始了,它会显示两次警告框。 我完全不知道为什么......所以任何想法都是?

我的应用程序有一个Application.js文件,它是整个angularJS应用程序的实际引导程序,而MainController.js是第一个'我的代码逻辑开始的控制器。

你可以在github上找到所有代码: https://github.com/jorisbrauns/Gloss/

注意:我还尝试将代码从此控制器中包装到

ons.ready(function() {
// But this had no effect on my alert, still twice ... :-(
})();

但是这里有一个简短的片段,我的警报被称为:

(function (app) {
    'use strict';
    app.controller('MainController', ['$scope', 'authService', function ($scope, authService) {

    //Retreive a object from localstorage
    $scope.authentication = authService.authentication; 

    //This is shown twice for an odd reason?
    alert("isAuth: " + $scope.authentication.isAuth + " / username: " + $scope.authentication.userName);

    //Figure out which page to show
    if (page.name == "") {
        if ($scope.authentication.isAuth) {
            _RedirectToMain();
        } else {
            $scope.mainNavigation.pushPage("views/login.html", {
                animation: 'none',
                onTransitionEnd: function() {}
            });
        }
    }
   // More code is here below, but not related to the issue (or not that i suspect it to be)

})(application);

1 个答案:

答案 0 :(得分:1)

经过数小时的搜索后解决: - )

我提出了这个解决方案:

var app = {
// Application Constructor
initialize: function() {
    window.application = window.angular.module('Gloss', ['onsen']);
    this.bindEvents();
},

// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline',  'online', 'pause' and etc
bindEvents: function() {
    document.addEventListener('load', this.onLoad, false);
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
onLoad: function() {

},

// deviceready Event Handler
onDeviceReady: function() {
    // If you like to have a splashscreen... (not needed for the solution, its fancy however)
    var parentElement = document.getElementById('deviceready');
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

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

    //Device is ready, load angularjs into the dom
    window.angular.bootstrap(document,['Gloss']);
}

};

app.initialize();

整个代码可用于: https://github.com/jorisbrauns/Gloss

确保以正确的顺序加载onsenui,angular和cordovo。 只需看看我的github index.html。