Can the function be executed without the need for ng-app?

After researching my AngularJS application, I discovered that having 2 ng-app tags in the html file means only the first one will be executed.

In my code snippet below

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js">
</script>
<script>
    var app = angular.module("MyApp", []);

     app.controller('myCtrl', function($scope, $http) {
        $http({
            method : "GET",
            url : "http://localhost:5050/get_time"
        }).then(function successCallBack(response) {
            $scope.jsonResponse = response.data;
         }, function errorCallBack(response) {
            $scope.jsonResponse = "failed"
         });
            angular.bootstrap(document.getElementById("App"), ['MyApp']);
      });
</script>
<div ng-app = "MyApp" ng-controller="myCtrl">

    <p>Response of JSON Below:</p>
    <h1>{{jsonResponse}}</h1>

</div>

I noticed this specific line here

<div ng-app = "MyApp" ng-controller="myCtrl">

This made me question if it's possible to make my function work without using ng-app. How can I achieve this?

Answer №1

To initiate a second application, utilize the angular.bootstrap method:

<div id="app2" ng-controller="myCtrl">    
    <p>Response of JSON Below:</p>
    <h1>{{jsonResponse}}</h1>    
</div>
angular.module("MyApp", [])
.controller('myCtrl', function($scope, $http) {
    $http({
        method : "GET",
        url : "http://localhost:5050/get_time"
    }).then(function successCallBack(response) {
        $scope.jsonResponse = response.data;
    }, function errorCallBack(response) {
        $scope.jsonResponse = "failed"
    });
});

angular.element(function () {
    angular.bootstrap(document.getElementById("app2"), ['MyApp']);
});

From the Documentation:

Key Points to Remember

There are several important considerations regardless of whether bootstrapping is automatic or manual:

  • Although it is feasible to bootstrap more than one AngularJS application per page, thorough testing has not been conducted on this scenario. Difficulties may arise, particularly with intricate apps, so caution is recommended.

For further details, refer to

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

If the progress bar in AngularJS reaches 100%, the "active" class will be automatically removed

How can I remove the bootstrap 'active' class from the progress bar once it reaches 100% to stop the stripes from moving? I tried this, but it's not working: <div class="progress progress-striped active" style="margin-bottom: 0;" n ...

Is each individual character displayed on the web page twice when using a controlled component?

Currently, I am diving into the extensive React documentation. A particular section caught my attention - controlled components. It delves into how form elements, such as an <input>, manage their own internal state. The recommendation is to utilize c ...

Can Socket.IO link to a source tag that doesn't actually exist?

As I was following the 'Get started thing' tutorial on Socket.IO, I encountered a step that required me to add the socket.io.js script to my HTML page. The instruction provided was: /socket.io/socket.io.js However, when I checked my folders, I ...

Is it accurate to consider all JavaScript code and variables as inherent properties of an execution context?

It's worth considering that everything in JS code can be viewed as a property of an execution context, whether it's a global, function, or eval() execution context. Why is this the case? Each execution context has its own unique lexical and v ...

Changing the color of a tooltip for a specific element using Bootstrap 4

Is there a way to customize the tooltip background-color for icons with the classes "alert-danger" to red and "alert-success" to green using the following CSS commands: .alert-danger + .tooltip > .tooltip-inner { background-color: red !important; } ...

Incorporate a Variety of Elements onto Your Webpage

I have developed a custom tooltip plugin using JQuery, and I am implementing it on multiple A tags. Each A tag should have a unique tooltip associated with it, so I have the following code: var $tooltip = $("<div>").attr("id", tooltip.id).attr("cla ...

Leveraging Toaster Notifications in AngularJS for Custom Exception Management and Logging

Implemented the use of AngularJS Toaster for effective notification handling. To customize exception handling, set up in index.html as shown below <toaster-container toaster-options="{'time-out': 3000, 'position-class': 'toast ...

Set AngularJS Material Design custom component back to its original state

I've developed a unique AngularJS custom component that acts as a wrapper for a Material Design mdSelect. It allows for easy configuration of available, default, and current values through its bindings. This component serves as an editing tool within ...

What is the process of sending a file from a remote URL as a GET response in a Node.js Express application?

Situation: I am working on a Multi-tier Node.js application with Express. The front end is hosted on an Azure website, and the back end data is retrieved from Parse. I have created a GET endpoint and I want the user to be able to download a file. If the f ...

Encountered an error stating "Cannot access property FindAll() of undefined" while working with a node/express JS app

Hey everyone, I'm encountering a problem with my express JS application and I'm not quite sure how to fix it. I'm working with an existing mySQL db and attempting to fetch items from my tbl_person table in the myDB database. As a newbie to t ...

Designing CSS elements that flow from top to bottom, left to right, and extend to the right when overflowing

I'm attempting to create a layout where divs are displayed from top to bottom, but once they reach the bottom of the browser window, any additional divs will overflow to the right. You can take a look at the desired layout here: https://i.stack.imgur. ...

Show data from an API in an HTML table

I encountered an issue with an API, and despite trying console.log(response.[""0""].body) to view the response in the console, it does not seem to be working. My goal is to extract all the data from the API and display it in a table. Below is my code: ...

Exploring the Potential of Mobile Development using AngularJS

I am in the process of creating an app with the following key design objectives: Efficiency and modularity - a light core that can be expanded to create a feature-rich app in a cohesive manner Mobile focus - this app is primarily aimed at mobile platform ...

Align the image in the middle using JavaScript for Firebase

I am struggling to display the center of an image within a circle-shaped <img> tag with a border-radius of 50%. The issue arises when trying to display an image fetched from Firebase storage, rather than from a URL. In attempt to solve this problem, ...

Angular and Express encountering a CORS 403 issue

My Express configuration includes settings that run on a different domain: app.use(function(req, res, next) { res.setHeader("Access-Control-Allow-Origin", 'http://localhost:3000'); res.setHeader("Access-Control-Allow-Credentials","true") ...

The system encountered an error stating that the required component "Footer" in the main directory was not located in the specified node_modules

Issue I'm Facing: Error found in components/main/Footer within the file path: ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue Components Tree as shown in the screenshot below: Cod ...

Implement a redux-form within a react-bootstrap modal

I am facing a challenge with incorporating a multipage 'redux-form' form into a react-bootstrap modal. My goal is to have the form displayed within the modal overlay when the modal is opened. How can this be achieved? The code below is producin ...

A strategy for concealing the selected button within a class of buttons with Vanilla JS HTML and CSS

I have encountered a challenging situation where I am using JavaScript to render data from a data.json file into HTML. Everything seems to be functioning correctly, as the JSON data is being successfully rendered into HTML using a loop, resulting in multip ...

Unlock the parent of an unnamed iframe

Here's a scenario: <div id="parent"> <iframe....></iframe> </div> If I had the above structure, I could use window.parent.document.getElementById('parent').innerHTML to access it. However, my current situation is di ...

Trouble with CSS and JS tabs not activating when clicked?

I am experiencing issues with a navigation (organized in tabs) that is not functioning on this page, but it works correctly on this page using the same method for inclusion. When clicking "Norway" on the top left, the navigation opens but switching to the ...