AngulasJS、OnsenUI、Phonegap、警报在控制器内显示两次
AngulasJS, OnsenUI, Phonegap, alert is showed twice inside a controller
我正在编写一个与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);
搜索数小时后解决:-)
我想出了这个解决方案:
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.
相关文章:
- Append元素在运行两次函数后不显示
- JavaScript倒计时计时器不会显示两次
- 使用wow.js显示两次动画对象
- Rails-jQuery 为什么我必须单击两次才能显示 jQuery 效果
- 下拉所选值在下拉选项中显示两次
- 下拉列表中的选定值在下拉列表中显示两次
- 警报显示两次而不是一次
- Chrome(也许是Safari?)激发了“;模糊”;当浏览器失去焦点时,在输入字段上显示两次
- AngulasJS、OnsenUI、Phonegap、警报在控制器内显示两次
- reactjs:为什么在render中定义的一些dom元素显示两次
- 为什么警告框在此代码中显示两次?
- 如何防止引导框在双击后显示两次
- Javascript警报显示两次
- Rails在Heroku上,编辑器显示两次
- startDate在FullCalendar插件中显示两次
- 为什么在我的应用中弹出警告显示两次
- 如何在页面上显示两次时,没有任何唯一标识符的目标特定的html
- 通过AJAX发布评论时显示两次,但在页面重新加载时只显示一次
- MeteorJS:表中的输入值显示两次
- 警告显示两次模态引导