Angular JS is experiencing issues with two modules not functioning properly on a single page

Angular JS is a new technology for me. I have two modules, first2a and first22. Each module contains a controller named model1 and model2.

Here is the HTML code:

<!DOCTYPE html>
    <html >
        <head>
            <link rel="icon" type="image/png" href="globe/images/correct.png"/>
            <link rel="stylesheet" type="text/css" href="globe/css/style.css"/>     
            <script type="text/javascript" src="globe/script/jquery.js"></script>
            <script type="text/javascript" src="globe/script/angular.js"></script>
            <script type="text/javascript" src="globe/script/mainscope1.js"></script>
            <script type="text/javascript" src="globe/script/angular-route.js"></script>
            <title>
                Html5 All in One
            </title>
        </head>
        <body >
            <div  ng-app="first22" id="maincontainer" > 
                <div ng-controller="model1">{{message}}</div>
            </div>
                    
            <div id="Interactive_content" ng-app="firsta" >
                <div ng-controller="model2">{{message}}</div>
            </div>
                        
        </body>
    </html>

mainscope1.js

var first2 = angular.module('first22', []);

    first2.controller('model1',function($scope)
    {
        $scope.message="aaaaaaa";

    })

var first2a=angular.module('firsta',[]);

    first2a.controller('model2',function($scope)
    {
        $scope.message="aaaaaaa1";

    })

I'm facing an issue with the firsta module not working properly. Can someone help me troubleshoot this? Thank you!

Answer №1

Consider utilizing Angular Bootstrap to trigger functions upon the document being fully loaded:

angular.element(document).ready(function() { 
    angular.bootstrap(document.getElementById('appContainer'), ['appModule']);
    angular.bootstrap(document.getElementById('InteractiveSection'), ['interactiveModule']);
});

Feel free to explore this example

Answer №2

Is your goal to have two separate web applications running concurrently on this page, or are you simply trying to incorporate two controllers within a single application? Remember, ng-app can only be used once per page.

If you prefer them to operate as one application (which is typically the case), ensure that both ng-controller directives are encapsulated within the same ng-app scope. This can be achieved by setting ng-app on the html-tag, body-tag, or a surrounding div element.

<!DOCTYPE html>
    <html >
        <head>
            <link rel="icon" type="image/png" href="globe/images/correct.png"/>
            <link rel="stylesheet" type="text/css" href="globe/css/style.css"/>     
            <script type="text/javascript" src="globe/script/jquery.js"></script>
            <script type="text/javascript" src="globe/script/angular.js"></script>
            <script type="text/javascript" src="globe/script/mainscope1.js"></script>
            <script type="text/javascript" src="globe/script/angular-route.js"></script>
            <title>
                Html5 All in One
            </title>
        </head>
        <body ng-app="myAngularApplication">
            <!-- Main page -->
                <div id="maincontainer" > 
                        <div ng-controller="model1">{{message}}</div>
                </div>
            <!-- Main page End-->   
            <!-- Interactivity page -->     
                <div id="Interactive_content" >
                    <div ng-controller="model2">{{message}}</div>
                </div>
            <!-- Interactivity page End-->          
        </body>
    </html>

Here is the corresponding JavaScript:

var first2 = angular.module('myAngularApplication', []);

    first2.controller('model1',function($scope)
    {
        $scope.message="aaaaaaa";

    });

    first2.controller('model2',function($scope)
    {
        $scope.message="aaaaaaa1";

    });

Alternatively, if your intention is to run two distinct applications on the same page, you will need to manually bootstrap them without ng-app. Refer to this Stack Overflow question for guidance on how to achieve this.

Answer №3

This question may have already been answered here:

According to the documentation, only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used as the root element for auto-bootstraping the application. If you need to run multiple applications in the same HTML document, you must manually bootstrap them using angular.bootstrap instead. It is not possible to nest AngularJS applications within each other. -- http://docs.angularjs.org/api/ng.directive:ngApp

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

Following the same occurrence using varying mouse clicks

I am currently exploring the most effective method for tracking file downloads (specifically, pdf files) on my website using Google Analytics (UA). I understand that by utilizing <a href="book.pdf" onClick="ga('send','event','P ...

Conceal a section of a container with the click of a button within a WordPress Plugin

I am currently utilizing a Wordpress plugin known as contact form 7 to construct an email list for an upcoming website project. The client has specifically requested that we avoid using services like mailchimp due to their preference of not sending ANY ema ...

The Forge Viewer's getState() function is providing inaccurate values for individual items

In our Angular application, we have integrated the latest version of Forge Viewer and are storing the current state of the viewer in our database for future restoration. After thorough testing, we discovered that isolated nodes are not being saved correct ...

What is the best way for a service to provide both data and multiple promises to a controller?

I've created a service with functions like this: angular.module('common').factory('_o', ['$angularCacheFactory', '$http', '$q', '$resource', '$timeout', '_u', ...

Error encountered when attempting to insert data into database due to incompatible data types

I am experiencing an issue with Vue multiselect. I am trying to save the id of selected options in a database, but it seems that I am encountering an error related to Array to string conversion. I have declared both site_id and administrator in fillables. ...

Unable to postpone the utilization of data in Vue until after retrieving the value from the database

I am facing an issue where I need to compare a string obtained from Firebase in document.check1 with specific strings (hardcoded in the function below) and display content accordingly. Currently, I know how to trigger this comparison on button click, but I ...

Is there a way for me to invoke a different function that is located external to

I am currently in the process of setting up an event that triggers on any change within the form input class. import React, { useState } from "react"; DealerRegistration extends React.Component { state = { business_type : 'd ...

Retrieving data from MySQL using PHP and AJAX with radio button selection

How can I update my code to display data from the database when a radio button is clicked instead of using a drop-down box? Here is an example of my current radio button code: <Input type='Radio' Name='compcase' value='1'/& ...

I'm working on separating the functionality to edit and delete entries on my CRM model, but I'm having trouble finding a way to connect these buttons with my data fields

I am encountering some difficulties while trying to implement separate functionality for editing and deleting items on my CRM model. I have already created the necessary API in Angular, but I am struggling to bind these buttons with my field. Any assistanc ...

Challenges with differentiating between addition and concatenation in Vue computed properties

Recently diving into Vue and came across an intriguing issue, I am curious to know the reason behind it and how to prevent it. <template> <div> <input type="number" v-model="a" style="color: white" /> <input type="number" v- ...

Dynamic Character Measurement

I am currently utilizing Datatables to dynamically add rows to a table with 3 columns: Index Text CharCount I am seeking logic to implement a character count for each entry in the 'Text' column and display it in the corresponding 'CharCou ...

I'm struggling to comprehend the purpose of this function. [From the Codecademy Contact List exercise]

Within the "for var x in friends" loop, the program aims to search each key within the friends object, such as bill and steve. Subsequently, with the condition "friends[x].firstName === name", the check is made if the first name matches the provided inpu ...

Utilizing AngularJS to bind data with labels and passing it in a form

I'm having trouble binding input data with label tags and using the same ng-model in a post method. Only one thing works at a time, and when I try to pass the label in the post method, it doesn't work. This issue is causing me difficulty in delet ...

Use AngularJS to take tab delimited text copied and pasted into a textarea, and then convert it into a table

One of the features in my app requires users to copy and paste tab delimited text. I need to convert this text into a table where each new column is represented by a tab and each new row is indicated by a new line. I've managed to create a list for e ...

Error: The OrbitControls function is not recognized in THREE.JS

const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const controls = new OrbitControls(camera); camera.position.set(200, 0, 0); controls.update(); const geometry = new THREE.S ...

Having trouble updating state in React after making a fetch request

I am encountering an issue with setting the current user after a successful login. Even though the login process is successful and the data is accurate, I am unable to set the state as the user data appears to be empty. UserContext.js import React, { useC ...

Ways to clear an individual form field using element-ui

I'm currently learning VueJS and incorporating the Element-UI framework into my project. One issue I'm facing is that when there's a validation error upon submitting the login form, I'd like to automatically clear the password field. A ...

What is the proper way to implement v-model for a custom component within the Vue render function?

Below is the code that I am working with: ... var label_key_map = { a: "1", b: "2", c: "3", d: "4" } render: (h) => { var form_data = {} for (let key in label_key_map) { var form_item = h( 'FormItem', {props: {prop: key}}, ...

Deactivate the Submit button when the database field has been populated

My form includes a submit button. The Submit button should be disabled if the "price" for a specific product is already filled: PHP Code <?php $host="localhost"; $username="root"; $password=""; $db_name="ge"; $con=mysqli_connect("$h ...

Issue with Flex property not being supported in Safari browser

.rq-search-container { position: relative; width: 100%; background: white; display: flex; flex-direction: row; align-items: flex-start; border-radius: 2px; z-index: 30; -webkit-flex: 1 0 15em; } While this code functi ...