The controller is unable to access the emitted data

I am having an issue with sending data from a service to a controller using $emit. When I log the data in the service, it is present, but when I try to access it in the controller using $rootScope.$on, the data is not showing up.

service.GetTest = function () {
    var inside = this;
    var promise = $http(
    {
        method: 'POST',
        url: inside.baseUrl + 'Admin/setTest',
        contentType: 'application/json'
    });
    promise.then(function (data) {
        console.log(data);
        $rootScope.$emit('monitor', data);
    })
}

$rootScope.$on('monitor', function (event,data) {
        console.log(data);
});

Answer №1

$on requires the callback function to have the event as the first parameter. Make sure your function is defined as function(event, data), not just function(data).

Additionally, it is recommended to return a promise from the GetTest function. Since it seems like a resource method, utilizing promise chaining would be more appropriate than relying on an event.

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

The dimensions of the body are set to 100vh in height and width. However, the div inside this body has a width of either 100vh or 100%, but it is not

I am trying to create a div element that has a width equal to the viewport of the browser. However, I am encountering issues when applying the CSS property width:100vh to the body element. Here is my code snippet: body { font-family: Staatliches; fo ...

AngularJS Services and Factories that are loaded at startup rather than on-demand

Having an issue with my AngularJS application. I have defined some factories that are injected into controllers as needed. The problem is that these factories are initialized when the application first starts up. One problematic service is: PlanningPoker ...

Strategies for managing data sharing between PHP and JavaScript

Today, I am seeking your valuable advice. Currently, I am working on a significant legacy project at my company, which involves finding a solution to share constants between our PHP 5 back-end (MVC architecture, no framework) and our front-end with Angula ...

Utilizing CakePHP 3.0 with jQuery UI for an autocomplete feature

Seeking assistance on why the current code isn't functioning. The objective is to retrieve data from the index controller to search and obtain JSON data. No requests are being made, and there are no visible results. New to CakePHP 3.0, I am attemptin ...

Algorithm for organizing pagination to ensure consistent page sizes and prevent excessively large or small pages

My goal is to create a pagination algorithm that distributes elements evenly per page, with specified minimum and maximum thresholds. I aim to maximize the number of elements per page while adhering to the minimum rule. I've attempted to develop my o ...

Increase the value of an array element based on the input from a specified label

In my JavaScript code, I am trying to increment the value of items by adding a label input from the user. I have set up an input label and button to receive and store the user input values. In my array, I have five items with unique Id's. However, I a ...

Activate/Deactivate toggle using Vue.js

new Vue({ el: '#app', data: { terms: false, fullname: false, mobile: false, area: false, city: false, }, computed: { isDisabled: function(){ return !this.terms && !this.fullname && !this.mob ...

JavaScript's concise ternary operation can be used for quick if/else statements when conducting

Here is my current if/else setup: async invoke(request, response) { const { deleted } = request.query; let users = []; if (deleted === 'true') { users = await User.findAndCountAll({ where: { ...

Storing Node.js console.log() in a text file with timestamp of creation

How can I save all the logs of my nodeJs program into a txt file without overwriting it each time? Currently, I am using node script-file.js >log-file.txt, but it just keeps replacing the content of the same txt file. Is there a way to automatically i ...

A guide to displaying a PDF preview using React Dropzone

I am struggling to find a way to display previews of PDF files that I'm uploading using react-dropzone. Although PNG and JPG files are working correctly, I would like to be able to show the user either the actual PDF or an image representation of it. ...

Exposing the full binary in jQuery

Can anyone explain how jQuery can display the entire binary representation of a number without removing the leading zeros? Here is the code snippet: HTML: <input id="input" type="text" size="20"> <input id="result" type="text" size="30"> < ...

Sending HTTP headers to a static directory in Connect (Node.js) can be achieved by manipulating the response object

Currently, I have a basic connect server set up to serve a directory. I am looking to add a custom HTTP header to all the files it serves. Below is my current code snippet: var connect = require('connect'); var app = connect() .use(conne ...

Combining Devise, Angular, and Rails 4: Issue with updating logged-in user without username/password validation

Almost there with the desired behavior! I have a project using Devise, AngularJS, and Rails 4 where the user login/logout functionality is almost perfect. I am able to send back the X-CSRF-Token and store the user's email in Angular as a cookie to pr ...

Strangely shaped border appears when interacting with editable div block

I am attempting to build a textarea that has the capability to display multiple colors. To achieve this, I created a div and used the following JavaScript code: element.unselectable = 'off'; element.contentEditable = true; Now, the div can be e ...

The issue with jQuery's .after() method is that it is inserting content as a string instead

Having trouble inserting a button element after another element selected using jQuery. Here's how I have my element selected: let btn = $("a[translate='server.ipmi.kvm.LAUNCH']")[0]; When I try to insert the button after it, I'm using ...

Bootstrap 4 Dropdown Menu not displaying on website

Hello everyone, I'm encountering an issue with Bootstrap v4-alpha6 where my dropdown-menu is not appearing as expected. I tried using a sample code from the Bootstrap documentation and it worked fine, but for some reason, my implementation does not e ...

How can I rearrange the output produced from a form by utilizing arrays?

The following code snippet generates the output: Apple,Orange Sliced,Diced Option1 Option2 Option3 Option4 ,Option1 Option2 Option3 Option4 Desired output: Apple Sliced Option1 Option2 Option3 Option4 Orange Diced Option1 Option2 Option ...

Removing items from a list with Node.js and Express using Mongoose

Having trouble deleting elements after retrieving them from the database with mongoose. I'm unsure how to select a specific element in the list for deletion. My app features a user list with corresponding ages. Check out my userview.ejs (updated afte ...

How can I conceal login and register router-links in the Vue + Laravel SPA project navbar immediately after a user logs in?

Currently, I am working on my Bachelor's degree project and have encountered a specific issue. While the login, register, and logout functions all seem to be working well, there is an inconsistency with the navigation bar not automatically switching b ...

Presenting the correct error notification if the JSON data fails to load in an HTML webpage

Here is a sample JSON data: [ { "componentid": 4, "displayImageUrl": "https://via.placeholder.com/350x200", "title": "theme", "shortdesc": "to set theme for different applications" }, { "componentid": ...