AngularJS allowing multiple ngApps on a single page with their own ngRoute configurations, dynamically loaded and initialized

Seeking advice on lazy loading separate ngApps on one page and bootstrapping them using angular.bootstrap, each with its own unique ngRoute definitions to prevent overlap.

I have a functioning plunkr example that appears to be working well, but I am unsure if this is the correct approach, hence reaching out for guidance.

<!DOCTYPE html>
<html>

<head>
  <!-- Latest CSS -->
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
<div class="container">
    <div class="col-xs-6 well" id="app1">
      <h3>App1</h3>
      <a href="#/app1/1">Route 1</a>
      <a href="#/app1/2">Route 2</a>
      <a href="#/app1/3">Route 3</a>
      <div class="well" ng-view></div>
    </div>


    <div class="col-xs-6 well" id="app2">
      <h3>App2</h3>
      <a href="#/app2/1">Route 1</a>
      <a href="#/app2/2">Route 2</a>
      <a href="#/app2/3">Route 3</a>
      <div class="well" ng-view></div>
    </div>
  </div>

  <script type="text/javascript" src="https://code.angularjs.org/1.2.21/angular.min.js"></script>
  <script type="text/javascript" src="https://code.angularjs.org/1.2.21/angular-route.min.js"></script>
  <script>
    var app1 = angular.module('app1', ['ngRoute']);
    app1
      .config(function($routeProvider) {
        $routeProvider
          .when('/:app/:param', {
            template: 'app = {{app}}, param = {{param}}',
            controller: 'Ctrl1'
          })
      })
      .controller("Ctrl1",['$scope','$routeParams', function($scope,$routeParams) {
        $scope.app = $routeParams.app;
        $scope.param = $routeParams.param;
      }]);

    var app2 = angular.module('app2', ['ngRoute']);
    app2
      .config(function($routeProvider) {
        $routeProvider
          .when('/:app/:param', {
            template: 'app = {{app}}, param = {{param}}',
            controller: 'Ctrl2'
          })
      })
      .controller("Ctrl2", ['$scope','$routeParams', function($scope,$routeParams) {
        $scope.app = $routeParams.app;
        $scope.param = $routeParams.param;
      }]);

    angular.bootstrap(document.getElementById("app1"), ['app1']);
    angular.bootstrap(document.getElementById("app2"), ['app2']);
  </script>



</body>

</html>

http://plnkr.co/edit/Y7A9bc3bDUyAXIS13JMZ?p=preview

FYI: Explored ui-router for loading separate states on the same page, but requires upfront source code loading with a single app.config containing all routes, which isn't ideal for a large project. Looking for a simpler, modular solution like the example above.

UPDATE in response to HockeyJ's question: The project has potential to grow significantly in terms of source code and module dependencies. Want to ensure each module can act as a standalone App, injectable at any point for testing purposes. Simultaneously, the entire project should operate as a single page app without screen reloads when loading App JavaScript files. Thus, adopting lazy loading of scripts on demand and bootstrapping apps to DOM elements. All Apps meet at URL routing with distinct naming conventions (e.g., /reports/whatever; /transactions/whatever) managed by ngRoute.

Answer №1

UPDATE:

Check out the overmind project designed for Angular, along with a demo available here

Explore a project divided into various apps: navigation, home, profile, and admin. The navigation app remains constant on the page and is the only app initialized upon pageload.

Simplify your Angular application by utilizing alternatives like sammy.js or history.js for client side routing instead.

Implement

angular.bootstrap(module, node)

within the routes of your sammy handlers

Consider integrating react.js (with or without flux) and combining it with sammy in a similar manner. For reference, check out this example from todombc: https://github.com/tastejs/todomvc/tree/gh-pages/architecture-examples/react

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

Streaming RTMP video through a WebView

Currently, I am working on implementing rtmp streaming using WebView. To achieve this, I referred to the code provided on Stack Overflow under this link The easiest way to play an audio RTMP stream in Android, which was shared by a user named Club. After a ...

Property-based Angular Material row grouping in a mat-table is a powerful feature that enhances

Is there a way to organize the data in one row with the same ID? Currently, my data looks like this: Data Set: { "id": "700", "desc": "Tempo", "richiesta": "20220087", "dataElab": &quo ...

Tips for reducing the JavaScript file size outputted by a liquid template generator

Currently, I am working on enhancing the performance of my Shopify website and GoogleSpeed Insights is suggesting that I minify css and js files. However, the recommended files are all created by the liquid template generator, making it challenging to uti ...

Differences in accessing the previous state between React's useCallback and useState's setState(prevState)

It has come to my attention that useCallback functions recreate themselves when their dependencies change, acting as a sort of wrapper for memoizing functions. This can be particularly useful for ensuring access to the most up-to-date state in useEffect ca ...

VueJS method for making an HTTP GET request

Attempting to make an http get request using Vue js. I can't seem to find any issues with the logic, although I'm not very experienced with vuejs. Continuously encountering these two errors: [Vue warn]: Error in mounted hook: "TypeError: Cann ...

The paragraph text should dynamically update upon clicking a button based on JavaScript code, however, the text remains unchanged despite attempts to modify it

I recently started learning JavaScript and wanted to update the content of a paragraph when a button is clicked. However, I encountered an issue where this functionality doesn't seem to work. <body> <p id="paragraph">Change Text on cl ...

Display requested tab feature using jQuery upon page load

I am new to jquery and have been using this code for creating tabs: <script type="text/javascript" charset="utf-8> $(function () { var tabContainers = $('div.tabs > div'); tabContainers.hide().filter(':first&apo ...

What is the most efficient way to update a large batch of documents in MongoDB?

I need to efficiently update a large number of documents (> 100,000). Initially, I attempted to do this on the JS level by writing scripts that fetch _ids first and then loop through them to invoke updates by _id (full docs or $set patches). However, ...

The terminal in VS CODE is not able to detect Stripe

When I attempt to run the command 'stripe listen' in the VS Code terminal, an error is thrown: The term 'stripe' is not recognized as the name of a cmdlet, function, script file, or operable program. Please check the spelling of the ...

Is it possible to simultaneously utilize a Node.js server and an ASP.NET web service?

Although I have experience in developing with .NET, I am new to Node.js and intrigued by its advantages. I believe it is a great way to maintain code and promote reusability. However, I am faced with a dilemma. I understand that Node.js allows for creatin ...

It's essential to ensure that this property remains responsive, whether through the data option or by initializing the property for class-based components

This code snippet contains the template and path information. <template> <div class=""> <c1 :text="message1"></c1> <c1 :text="message2"></c1> </div> </templa ...

Is it possible to utilize $regex alongside $all in mongoDB?

In my current project, I am facing a challenge where I need to handle an array of strings received from the frontend. Each document in my mongoDB database also has its own array of keywords. The tricky part is that the strings sent from the frontend migh ...

Uncover the reason behind the application's crash with Titanium

If we encounter non-crashing errors, utilizing LogCatcher can help by pinpointing which Javascript code is causing the issue. However, in the event of a crash, there's no time for logging Javascript errors. In such cases, integrating tools like ARCA ...

Tips for iterating through an array of objects nested within other objects

Good afternoon everyone, I’m having trouble accessing the attributes of a JavaScript object array through AJAX. Can anyone spot where I might be going wrong? View image description This is my AJAX call: $("#cbx1").on("change", func ...

Calling http.get one after the other without knowing the length of the list

Suppose I have a list of URLs as follows: var myurls = ['http://server1.com', 'http://server2.com', 'http:sever2.com', etc ] Each URL is considered a "fallback" and should only be used if the previous one is unreachable. Thi ...

The syntax in JavaScript of (0, fn)(args) is used to call

I came across an interesting piece of JavaScript code in Google's codebase that I wanted to discuss: var myVar = function...; (0, myVar)(args); Do you know what the significance of this syntax is? I'm struggling to see the difference between ( ...

Instructions on incorporating domains into next.config.js for the "next/image" function with the help of a plugin

Here is the configuration I am currently using. // next.config.js const withImages = require("next-images"); module.exports = withImages({ webpack(config, options) { return config; }, }); I need to include this code in order to allow images from ...

Is it necessary to insert a thread sleep in HtmlUnit before clicking a button?

I have been experimenting with HtmlUnit to extract scores from the BBC Sports website Upon loading the page, it initially displays Premier League scores. To view scores for other leagues, one must use a dropdown menu and click the 'Update' butto ...

Is it possible for me to take action on and then pass along the outcomes of an AngularJS $http request without relying on $q?

I have a function called getData that retrieves data from an API endpoint. My current implementation utilizes $q to handle promises. After processing the retrieved data, I return another promise: var getData = function (controller) { var defer = $q.d ...

What is the best way to send an XML body using Angular 5 with POST method?

I am currently developing an Ionic application that needs to make REST API calls with XML as the body. However, I am facing issues in getting the call to post with XML. This is my LoginProvider where I utilize DOMParser to parse data to XML before sending ...