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

Executing AngularJS code that returns a promise within an event

In my run function, I am handling the $routeChangeSuccess event. My goal is to use $http to load some data and then change the $template. The issue here is that $http works asynchronously. I have attempted to return a promise inside the event, but it has ...

Ways to automatically close the external window upon logging out in Angular 12

I have successfully created an external window in my Angular application. Everything is working as expected, but I am facing an issue when trying to automatically close the external window upon user logout. Although I have written the code below and it wo ...

Incorporate an HTML form into the primary HTML webpage using AngularJS application technique

As a newcomer to AngularJS, I am attempting to grasp how it can be integrated with an ASP.NET-MVC5 application. For my initial test, I want to launch an AngularJS app from a basic HTML page (in this case index.html) where the app contains a form template i ...

Obtaining the URL of the Parent Page Using Javascript

I encountered a situation in which, when I open a modal window dialog from Page1.aspx, if a user attempts to directly open that dialog by copying the URL and pasting it into the browser, I want to prevent the modal window from opening. It should only open ...

Guide on extracting data from a JavaScript table using Python

Looking to extract information from a table on this page: . There are a total of 18 pages, but the url remains constant for each page. My usual method involves using BeautifulSoup to scrape data from HTML pages. However, in this case, the required data is ...

Having trouble accessing the card number, expiration date, and CVC in vue using Stripe

Although I am aware that Stripe securely stores all information and there is no need for me to store it on my end, I have a specific requirement. When a user begins typing in the input area, I want to capture and access the card number, expiry date, and ...

Problems arising from jQuery Mobile, NPM, and WebPack

After spending a considerable amount of time researching and experimenting, I am confident that I could piece something together to make it work. However, I would rather gain an understanding of the root cause of my issue. It is widely acknowledged that j ...

Issues with jQuery slide operation

I'm facing an issue with jQuery and I can't figure out where it's coming from. Here is the error message that keeps showing up in the console: Uncaught TypeError: Object [object Object] has no method 'getElement' script_16.js:46Un ...

Incorrect footer navigation on my Vuepress website

I'm in the process of developing a vuepress single page application for showcasing and providing downloads of my personal game projects. When it comes to website navigation, I am aiming to utilize the native sidebar exclusively. This sidebar will hav ...

Having difficulty displaying form errors using handlebars

My form validation is not working properly. When I enter incorrect information, it alerts correctly, but when I submit the form, it returns [Object object]. What could be causing this issue in my code and how should I handle the data? https://i.stack.imgu ...

Resize the main container to fit the floated elements

I've been working on constructing a family tree, and the last part of the functionality is proving to be quite challenging for me. My family tree consists of list elements that are all floated to the left. Currently, when the tree expands beyond the ...

Sending a JavaScript object as a prop in a React component

Currently, I am enrolled in a React course that requires us to pass a single JavaScript object as props to a React application. Here's the code snippet I have been working on: import React from 'react'; import ReactDOM from 'react-dom& ...

Combining arrays of objects in VueJS

I am working with 2 components: parent component (using vue-bootstrap modal with a vue-bootstrap table) child component (utilizing vue-bootstrap modal with a form) Issue: When I submit the form in the child component, it successfully adds the object to ...

Guide to setting up an object array in JavaScript with unique child elements and no duplicate values

I have an array that looks like this: sampleArray = ["x", "y", "z". "a.b", "a.b.c", "a.b.d", "a.e.f", "a.e.g"] However, I am aiming to transform it into the following structure, or something similar to it: newArray: [ { "x": [] }, ...

Adding a new column to a table that includes a span element within the td element

I am attempting to add a table column to a table row using the code below: var row2 = $("<tr class='header' />").attr("id", "SiteRow"); row2.append($("<td id='FirstRowSite'><span><img id='Plus' s ...

multiple elements sharing identical CSS styling

For each component, I made separate CSS files and imported them accordingly. However, despite the individual styling efforts, all components seem to have the same appearance. I specifically worked on styling an image differently for two components, but w ...

Capturing mouse clicks in Javascript: a guide to detecting when the mouse moves between mousedown and mouseup events

I have been working on a website that features a scrolling JavaScript timeline, inspired by the code found in this tutorial. You can check out the demo for this tutorial here. One issue I've encountered is when a user attempts to drag the timeline an ...

One important rule to remember when using React Hooks is that they must be called consistently and in the correct order in each render of the component. It

Encountering an issue while trying to use the ternary operator to determine which React Hook to utilize. The error message states: "React Hook "useIsolationDynamicPhase" is called conditionally. React Hooks must be invoked in the same order during every co ...

How to Monitor Store Changes within a Vue File Using Vue.js

I am working with 2 vue files, header.vue and sidebar.vue. Both components are imported into layout.vue. Here are the steps I am following: 1. Initially, when the page loads, I update the store in header.vue with some values inside the created hook. 2. ...

The date range picker displays the previous arrow but not the next arrow

I am currently using the DateRangePicker tool and for some reason, I am not seeing the arrow that should appear on the right side. I have double-checked my configuration but can't seem to figure out what is causing this issue. In the image attached, ...