I am experiencing difficulties with broadcasting and attending events

File.js

scope.menuItemClick = function (def, callbackText, keepMenuOpen) {

  console.log(def);

  if(def.automationId === "Print"){

  console.log("before send");

  $scope.$root.$broadcast("printingData","my Data");

  console.log("after send");

}

Next.js

$scope.$on("printingData",(event, data) => {

  console.log(data);

  console.log("receiving");

});

I am facing an issue where I am unable to receive the broadcasted event even though I am broadcasting it from the root scope. Can someone provide assistance? Thank you.

Answer №1

If you want to broadcast events, it's recommended to utilize $rootScope. Simply inject $rootScope into your controller in the same way you inject $scope.

Example for Controller 1:

$rootScope.$broadcast("printForms", "myData");

Example for Controller 2:

$rootScope.$on("printForms", function(event, data){
    console.log(data);
});

Answer №2

It's important to examine the relationship between your two controllers. Identify which one is the parent and which one is the child. If both controllers are running simultaneously, determine which one loads first.

The primary reason for it not functioning properly could be that your second controller is loading after you have called

$scope.$root.$broadcast("printingForms","my Data");

Furthermore, while $scope.$root does reference the $rootScope, it is advisable to use $rootScope instead.

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

Can anyone suggest a way to iterate over an object and substitute spaces in the keys?

I need to iterate through a dynamically-created object that contains properties with values. The issue is that the property names in this dynamic object contain spaces, which are not allowed in JavaScript object properties. How can I loop through this ob ...

Having trouble deciphering mathematical formulas while editing content on ckeditor

While using math formulas in CKEditor, I noticed that when I insert new content via textarea, the formulas are displayed correctly. However, when I go back to edit the content, the text formulas do not display as before. This is the source code I am using ...

Exploring a JSON object using PlaywrightWould you like to know how

Greetings! Here is a snippet of code that I have, which initiates an API call to a specific URL. const [response] = await Promise.all([ page.waitForResponse(res => res.status() ==200 && res.url() == & ...

What is the best way to include multiple entries in a select2 form using ajaxform?

select2/3.5.2/ I had to repost this because my initial post was not formatting correctly. The tools in use are: A select2 form field that allows searching through multiple records A bootstrap popup modal containing a form for entering a new record if i ...

The functionality of fetching website titles using Ajax/jQuery is experiencing some limitations

Below is a code snippet for a simple website title getter: $( document ).ready(function() { $("#title").click(function() { var SubURL = $("#input").val(); $.ajax({ url: "http://textance.herokuapp.com/title/"+SubURL+"/", complete: function(da ...

exchange information among distinct domains

Two distinct services are operating on separate machines, resulting in different URLs for each service. The user initially accesses the front end of the first service. Upon clicking a button on this service, the user is directed to another front end servi ...

Deactivate the button using AngularJS

I currently have two buttons on my webpage: "Save Set" and "Load Set". The "Save Set" button is disabled using the ng-disabled directive, which calls the function isSaveDisabled() ..... .controller('saveLoadSetToolbarBtn', ['$scope',&a ...

I encountered login issues when trying to access dist/index.html in my Angular 8 application, but I found a workaround by utilizing the proxy.conf.json file, which

I have been trying to login through the index.html page in the angular 8 dist folder, but I keep encountering an error with the login API URL. Despite spending two days on this issue, I have not been able to find a solution. If anyone has any suggestions o ...

Inspect each page to confirm that the user has verified that they are of legal age

I'm currently developing a website (tobacco-related) that imposes an age validation requirement on all visitors before granting access to the site. I've implemented a form for age verification, but I'm stuck at how to store a cookie upon pas ...

A different approach for dynamically displaying React components sourced from an API

As I develop a website using Next.js/React that pulls in content from Strapi CMS, my goal is to create a dynamic page template for news articles. The aim is to empower content editors by giving them the flexibility to choose the type of content they wish t ...

Tips for excluding the HTTP OPTIONS request from being intercepted by the express.js authentication middleware

I have set up a backend API using express.js that interacts with an AngularJS single-page application. To secure access to specific resources, I am utilizing token authentication to verify clients. In order to validate whether the incoming request from th ...

Issue with Angularjs where changes to the view value are not reflected in the display

I have added an input directive for users to be able to undo actions. When the Enter key is pressed, the value is saved using a specific function, and pressing Esc should cancel edits and revert to the last saved value. However, I'm encountering an ...

Leveraging property information for a dropdown field

In my index.tsx file, I am fetching data that I pass to the component FormOne. This allows me to access the data in FormOne using props.data const [data, setData] = useState([]); ... return ( ... <FormOne data={data} /> ); I am looking to ...

Issues with custom URLs in AngularJS $resource not functioning as expected

Currently, I have set up two services in my application: .factory('HttpHandler', function() { return { loadData: function (promise) { var self = { data: [], loading: true, ...

Custom HTML on Joomla causing carousel to vanish

I've encountered an issue with my Joomla website where my carousel images are disappearing. I have a code snippet from W3 Schools for an automatic banner that works perfectly fine when opened in a browser using Notepad++, but when inserted into a cust ...

The input argument must be of type 'PollModel', as the property 'pollId' is required and missing in the provided 'any[]' type

Hey there! An issue popped up when I tried to pass an empty array as a model in my Angular project. The error message reads: "Argument of type 'any[]' is not assignable to parameter of type 'PollModel'. Property 'pollId' is ...

Steps for assigning a texture to a child Mesh within an Object3D

My Object3D was imported in the following manner: var customObject; // defining a global object var loader = new THREE.ObjectLoader(); loader.load('path/to/object3d.json', function(object) { customObject = object; scene.add(customObject ...

Using AngularJS to pass the output of a unique filter to another custom filter

I have successfully developed two custom filters and am attempting to utilize them both within an ng-repeat loop. Is there a way for me to pass the output of the first filter as an input for the second one? I attempted using 'as' keyword in ng- ...

Preserve the location of a moveable div using jQuery

I have implemented draggable divs in my JSP page, allowing users to move them around freely. After dragging the divs, I would like to save their positions either in a cookie or database for future reference. Could you please advise on the best way to ach ...

Upon refreshing the page, next.js 13's useSession() function fails to fetch session information

Currently, I am working on replicating an ecommerce site using nextjs 13. On the order page, I am utilizing useSession from next-auth/react to check if a user is signed in or not. Everything works fine when I navigate to the page through a link, but if I r ...