Twice within a controller, alert is displayed in AngulasJS, OnsenUI, and Phonegap

I'm currently developing a mobile application using AngularJS, OnsenUI, and PhoneGap to package it for Android devices.

However, I've encountered a strange issue where an alert box pops up twice when the application is run as a new process. I'm completely stumped as to why this is happening...any thoughts or suggestions?

The core of my app lies in the Application.js file, which serves as the main bootstrap for the entire AngularJS application. The logic of my code begins in MainController.js, the primary controller.

If you'd like to take a look at the code, it can be found on GitHub: https://github.com/jorisbrauns/Gloss/

Note: I attempted wrapping the code within this controller with ons.ready(function() { // However, this did not resolve the duplicate alert issue ... :-( })();

Below is a snippet where the alert is being triggered:

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

    //Retrieve object from local storage
    $scope.authentication = authService.authentication; 

    //This alert seems to appear twice mysteriously
    alert("isAuth: " + $scope.authentication.isAuth + " / username: " + $scope.authentication.userName);

    //Determine which page to display
    if (page.name == "") {
        if ($scope.authentication.isAuth) {
            _RedirectToMain();
        } else {
            $scope.mainNavigation.pushPage("views/login.html", {
                animation: 'none',
                onTransitionEnd: function() {}
            });
        }
    }
   // Additional code continues below, unrelated to the suspected issue

})(application);

Answer №1

After hours of diligent searching, I finally found the solution to my problem :-)

Here is the code snippet that worked for me:

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

// Bind necessary events for startup
bindEvents: function() {
    document.addEventListener('load', this.onLoad, false);
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
onLoad: function() {

},

// Event handler for deviceready
onDeviceReady: function() {
    // If you want a splashscreen (not required for this solution, but adds flair)
    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();

You can find the complete code on: https://github.com/jorisbrauns/Gloss

Ensure that you load onsenui, angular, and cordova in the correct sequence. Refer to the index.html file on my GitHub repository.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a structure object in a Laravel controller for implementing Vue.js autocomplete functionality

I am currently facing an issue with my autocomplete feature not displaying options correctly. To start off, I am fetching hard coded data from my controller and passing it through an axios call: searchController.php $searchResults = [ 0 => (ob ...

Issue with the status of a vue data object within a component

While working on updating my original Vue project, I encountered an error with the data object sports_feeds_boxscores_*. The website features three tabs that display scores for the major sports leagues. I am currently in the process of adding player stats ...

Exiting a NodeJs function entirely rather than just returning from an internal function

There is a function in my code app.post('/assignment/loan', (req, res) => { Within that function, there is another function db.run('SELECT loanable FROM book WHERE id=?',[bookID],(err,row)=>{ I tried using return but it only exi ...

Error: The global variable cannot be emptied due to an issue with the Ajax request

As someone who is relatively new to the world of Javascript/jquery and async, I have spent a significant amount of time reading through various forums. Unfortunately, I have yet to come across a solution that addresses my specific issue. The problem at ha ...

Sharing a boolean state between two pages using Material UI components

One challenge I've been facing is passing a boolean useState value between two pages. I found a helpful solution on this Stack Overflow thread: How to call useState from another Page? The main goal here is to display a success alert on a separate pag ...

Executing a prop function within the useEffect hook: a step-by-step guide

I am attempting to address this warning in a react component Line 19:8: React Hook useEffect has a missing dependency: 'handleChange'. Either include it or remove the dependency array react-hooks/exhaustive-deps This is the component: ...

You won't find the property 'includes' on a type of 'string[]' even if you're using ES7 features

I encountered a similar issue on another page where it was suggested to modify the lib in tsconfig.josn. However, even after changing compile to es7, the same error kept appearing and the project couldn't be compiled or built. { "compileOnSave": ...

How can you utilize a computed property in a Vue component to select all the text within it?

When using $event.target.select(), I usually can select all the text. However, in this scenario, it seems to be selecting everything and then replacing the selection with the computed property. How can I select all after the computed property has finished? ...

Not all of the data is being retrieved

A form on the page contains two dropdown lists for selecting blood type and city, along with a button. When the button is clicked, information about donors matching the selected blood type and city will be displayed below the form. The postgres database co ...

Changing an HTML file formatted with Jquery into a Phonegap compatible version

I am working on converting my HTML file into an Android app using phonegap. However, I have encountered an issue where the buttons for showing/hiding images using JQuery have stopped functioning properly. Despite linking both the normal jQuery file and th ...

Checking for the presence of text within a span tag - a simple guide

see fiddle if there is an already allotted time slot for the patient, change its color to yellow using jquery on the client side. In my example, if I assign a time slot for the first time, it turns green and when assigning another one, the previous slot tu ...

Creating components through the command line while managing multiple projects using Angular CLI 6

Currently, I am utilizing the most recent Angular CLI version (v6). Within my codebase resides a collection of applications housed within the projects directory. My objective is to create and organize various modules and components within these projects v ...

Displaying variables as HTML in Node.js involves rendering the variable data

Currently experimenting with displaying a Node.js variable as HTML. This is my setup in app.js using Express: Package.json "dependencies": { "body-parser": "~1.12.0", "cookie-parser": "~1.3.4", "debug": "~2.1.1", "express": "~4.12.2", ...

The JSON array successfully retrieves the data, however, it is unable to be formatted and displayed

I am having trouble displaying JSON data from an online source in my app. Although the data is being retrieved successfully, it does not appear on the activity where it should be displayed. I suspect there might be an issue with how I am populating the lis ...

Comparing obj.hasOwnProperty(key) with directly accessing obj[key]

Consider the scenario where I need to determine if a property exists within an Object. A comparison between two methods caught my attention: if(object.hasOwnProperty(key)) { /* perform this action */ } OR if(object[key]) { /* perform this action */ ...

Use jQuery to change the background color when clicked

Below is the HTML code with UL and LI elements: <UL> <LI><span id='select1'>Text</span></LI> <LI><span id='select2'>Text</span></LI> <LI><span id='select3'>Tex ...

Ways to retrieve the identifier of an iframe

document.getElementById(Iframe_id).contentWindow.addEventListener("blur", blurtest, true); This line of code assigns the blur event to an iframe and it is functioning properly. However, when in function blurtest(e) { alert(e.target.id); } An alert ...

Solutions for resolving the issue of not being able to load images when using merged-images in JavaScript

I have a base64 image here and it has already been converted. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUTEhQWFhUXGBoaGBgYGBcXGhgXGBgYGhgbHhoZHiggGholHhgYITEhJSkrLi4uHR8zODMtNygtLisBCgoKDg0OGhAQGi0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0 ...

How can I send arguments to limit the use of 'E' directives in order to create a dynamic template?

I'm looking to convert some HTML into a directive but I am unsure about how to create directives with dynamic templates. My goal is to pass parameters to the template so that it can be reused with different values. Currently, I have passed parameters ...

Refresh numerous HTML <div>'s using data from a JSON object

Looking to automatically update the homepage of my website with the ten most recent "posts" from the server. Here's an example HTML format: <div id="post1"> <p id="link"> </p> </div> <div id="post2"> <p id="li ...