Sharing a scope variable among ng-app modules in AngularJS

Is it possible to have two separate ng-apps running on the same page?

  1. bookingApp
  2. calendarApp

I am looking for a way to share a selected date scope variable from calendarApp with bookingApp. Any ideas on how to achieve this functionality?

Thank you, Gladiator

Answer №1

To achieve this goal, you'll need to utilize an angularjs service. Below is a suggested implementation for reference:

angular.module('app.C', [])
.service('ServiceC', function() {
    this.getData = function() {
        return this.myData;
    };

    this.setData = function(newData) {
        this.myData = newData;
    }
});

angular.module('app.D', ['app.C'])
.service('ServiceD', function(ServiceC) {
    this.getData = function() {
        return ServiceC.getData();
    };

    this.setData = function() {
        ServiceC.setData('Updated data');
    }
});

Answer №2

To exchange information between Angular applications, consider utilizing the Window postMessage method and/or the localStorage.

Encapsulate the usage of postMessage within an Angular Service to ensure smooth communication and handle changes triggered by post events.

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

When working with the Google Sheets API, an error occurred: "this.http.put(...).map is not a valid

Having difficulty with a straightforward request to the Google Sheets API using the PUT method. I followed the syntax for http.put, but an error keeps popping up: this.http.put(...).map is not a function. Here's my code snippet: return this.http ...

Can you explain the inner workings of the sort function in JavaScript, including how it utilizes the compare

I'm curious about how the sort function operates in JavaScript, specifically in conjunction with the compare function. According to what I've read, if you have an array and use the code array.sort(compare), it's stated that if the compare fu ...

Placing miniature vessels within a larger vessel, where two small containers fit on one level of the larger container (refer to images)

I am looking for a way for users to input text in the "your text" container and then click "add". This action should create a new container within the larger red-lined container with the information provided by the user. I know how to do this already, but ...

Error: Property cannot be read after page refresh or modification

Upon refreshing or running the project for the first time, I encounter the error: TypeError: Cannot read property 'statements' of undefined This issue is perplexing as the data renders correctly but it appears that the connection is failing. ...

The JSON element is failing to render

I recently started using ejs and encountered an issue with my input tag: <input class="form-control" id="inputName" type="text" <% { %>value='<%= JSON.stringify(result.firstname) %>'<% } %> When I try to print the data, it ...

Preserve the ajax controls while uploading files

I encountered a minor issue with file uploading. When I click the upload button, a window (ajax powered) pops up for selecting a file. However, as soon as the file is uploaded to the browser, the page reloads and the window closes, leaving me without the ...

Tips for implementing a confirmation dialogue box prior to submitting a form:

As a new developer, I am facing an issue that I believe you can help me with. I want to display a confirmation box before deleting. How can I achieve this in my Ajax code? HTML: <input type='button' class="btn btn-danger delete_button" id="de ...

Creating unique ID tags using AngularJS

I am struggling with creating an HTML structure that looks like this: <ul> <li id="r1_1">Root node 1 <ul> <li id="child_node_1">Child node 1</li> <li id="child_node_2">Child node 2</ ...

The file-loader in Webpack is failing to copy the files and update the URL

I am encountering an issue with webpack file loader where it is not outputting image files. This problem arises while also importing HTML files as partials in my Angular app. Below is my webpack configuration for handling images using the file-loader: [w ...

"Maps and Ripple emulator are effective tools for development, but the android emulator does

I attempted to implement a Google Maps code for Cordoba using two different emulators - Ripple Chrome and the standard Android emulator. Surprisingly, Ripple was successful in displaying a geographic location on the map, whereas the Android emulator only ...

How to preserve alternating row styles when exporting Angular Data Table to Excel with .withButtons?

Utilizing Angular DataTable to display a list of data, with alternate row background color and border styles defined. An issue arises when exporting the Data table to excel as the alternate row style and border styles are lost. Only the raw data is includ ...

The Quasar application does not eliminate console.log() statements during production builds

I've been facing difficulties in removing the console.log() from my Quasar app for production builds. Despite checking solutions on various platforms like StackOverflow, Quasar forums, and GitHub, I am still struggling to eliminate the console.log st ...

Dynamic translation routing with Next.js

My goal is to create dynamic translation routes for specific pages in next js using i18next. While the following code works well, it currently routes to all pages and generates errors during next build. const router = useRouter(); const path = router.route ...

Create a 3D rendering of an .obj file using Three.js without relying on the

I have a challenge of drawing a .obj file without loading it. Let's consider an example where the .obj file contains the following content: v 0.1 0.2 0.3 v 0.2 0.1 0.5 vt 0.5 -1.3 vn 0.7 0.0 0.7 f 1 2 3 By reading and parsing this file, I am able to ...

Modify a JSON value while iterating through a forEach loop

As I am in the process of setting up an API for my server, I encountered a challenge while attempting to update a value within a JSON structure containing country codes and numerical values. My approach involved iterating through an array of JSON data fetc ...

Using Angular in my workflow significantly increases the amount of static code within my HTML files

I am currently testing my app on a live Rails server and encountering issues with Angular integration. To better examine the code, I have disabled config.assets.js_compressor = :uglifier in production.rb file. The challenge lies in the fact that Angular ...

Tips for showcasing the latest Google Map InfoWindows above previous ones

I have a dynamic Google Map with InfoWindows being added dynamically, but I am facing an issue where overlapping InfoWindows do not always display the most recent one on top of older ones. Is there a way to ensure that the latest InfoWindow always shows u ...

Tips for saving React console logs to a server

We are looking to understand the root cause of the issue occurring on the client side and figure out a way to store client console logs on the server side. How often should we send these logs to the server for storage? We also want to make sure that when ...

JavaScript's counter variable can become confusing when used in an if statement

Struggling as a beginner in javascript, I find myself stuck on writing an If statement that triggers after the fourth turn. My goal is to have an alert pop up once the user has clicked on four options. The counter variable "turns" was added to track prog ...

Implementing context menus on the Material-UI DataGrid is a straightforward process that can enhance the user experience

I am looking to enhance my context menus to be more like what is demonstrated here: Currently, I have only been able to achieve something similar to this example: https://codesandbox.io/s/xenodochial-snow-pz1fr?file=/src/DataGridTest.tsx The contextmenu ...