Combining Angular2 and Angular1 with ES5 syntax for component subscriptions

Currently, I am still using ES5 for my frontend Google Apps Scripts application as I haven't had the time to transition to TypeScript. As part of upgrading my Angular1 app to Angular2, I came across this method:

To handle loading states and overlay divs, I created an overlayLoaderService service with functions to manipulate loading state and an overlayLoader component to display the div.

Service:

var overlayLoaderService = ng.core.
  Injectable().
  Class({
    constructor: function() {
      this.loading = false;
      this.stateChange = new ng.core.EventEmitter();
    },
    setState: function(state) {
      this.loading.value = state;
      this.stateChange.emit(state);
    },
    getState: function() {
      console.log(this.loading);
    }
  });

upgradeAdapter.addProvider(overlayLoaderService);
angular.module('Gojira').factory('overlayLoaderService', upgradeAdapter.downgradeNg2Provider(overlayLoaderService));

Component:

var OverlayLoaderComponent = ng.core
  .Component({
    selector: 'overlay-loader',
    template: '<div [hidden]="loading" id="overlay-loader"></div>',
    providers: [overlayLoaderService]
  }).Class({
    constructor: [overlayLoaderService, function(overlayLoaderService) {
      this.loading = !overlayLoaderService.loading.value;
      this._subscription = overlayLoaderService.stateChange.subscribe(function (value) {
        console.log(value);
        this.loading = !value;
      });
    }],
  });

angular.module('Gojira').directive('overlayLoader', upgradeAdapter.downgradeNg2Component(OverlayLoaderComponent));

I am trying to make the component update its loading property when the setState() method in the overlayLoaderService is called. However, the subscription is not triggering, indicating a mistake on my end.

Any assistance would be greatly appreciated.

Answer №1

delete

providers: [overlayLoaderService]

Removing the above code from OverlayLoaderComponent should resolve the issue, as the provider is already included in the adapter. Otherwise, there may be conflicts between angular 1 and angular 2 components using different instances of the service.

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

Tips for dynamically updating the formatting of a specific word within an HTML paragraph using code?

I am looking for a way to dynamically change the appearance of a specific word within an HTML paragraph let textToFind = "testing" let beforeNode = document.createElement('p'); let afterNode = document.createElement('p'); let ...

I am having trouble with setInterval not functioning as expected. I am trying to make a function repeat every 500ms using setInterval, but it seems to be ineffective

var direction; function movement(){ switch(direction){ case 1: positionX = positionX + 10; break; case 2: positionX = positionX - 10; break; case 3: positionY = positionY - 10; bre ...

Ways to disseminate arguments when dealing with an array of arrays in JavaScript

Struggling to pass an array as arguments into the join method on path in node, but hitting a roadblock: var path = require("path"); var paths = [__dirname]; var userInput = ["app", "js"]; paths.push(userInput); var target = path.join.apply(null, paths); ...

Troubleshooting React Table cell issue with MUI integration: A step-by-step guide

Having just started working with React, I decided to create a table component using React Table-MUI. However, when attempting to place my table row within <Box></Box>, an issue arose where the entire table body was only occupying the space of t ...

Implementing pagination in an express application

I am faced with a situation where I have an array of JSON objects generated by a function in my running Express app. When there is only one object, it is rendered using the following Jade view. p You searched for '#{prop}' h2 Result if res ...

What exactly does the combination of {on, attrs} create within Vue/Vuetify?

Although this question may have been asked before, I am still struggling to grasp its meaning. Can you help me understand it better? <v-dialog v-model="dialog" width="500" > <template v-slot:activator=&quo ...

Clicking on "Ng-Click" will add a fresh row to the table using Angular

Is there a way to insert a new row into a table using ng-click? I currently have the following setup with the data stored in an array. Here is how my array looks. $scope.workflows = [{ Id: 1, Name: "Workflow Page 1", ...

What is the best way to find the most commonly used category for a product in a MongoDB collection?

I've been diving into MongoDB and encountered a challenge with my current task. I'm trying to figure out how to determine the most frequently used category in a collection. According to this JSON, the most used category is CIES. My goal is to dis ...

I am interested in incorporating the ability to select and scroll the window without needing to interact with the scroll bar by

Imagine a visitor wanting to highlight all the information on a webpage. They choose to start dragging their cursor towards the bottom of the window. How can we enable the webpage to smoothly scroll down as they do this? ...

User should clear the cache to accommodate the latest release

After each release, users are required to manually clear their cache in order for the new code to be applied. Is there a feature or method that could automate this process? ...

Generate a PDF document containing an image from a document within a MongoDB database using js

//SCENARIO 1 let onlineImage = `someURL.jpg` doc.addImage(onlineImage , "JPEG", 30, 20, 50, 50); //SCENARIO 2 let image = `${baseURL}/${logo}`; //image fetched from mongodb doc.addImage(image, "JPEG", 30, 80, 50, 50); For SCENARIO 1, ...

Finding an element on a webpage by its innerHTML

Currently, I am tackling a project that involves customers sending inner HTML in an Excel file for me to verify its visibility on a webpage. My current method is to paste the HTML into the Chrome developer console and manually check if the element is prese ...

Symfony2 encountering difficulties in locating file after deployment

Upon launching my project on the live server, I encountered the following error: An error occurred during template compilation ("Could not locate file "@JDareClankBundle/Resources/public/js/".") in "JDareClankBundle::client.html.twig". I have tried clear ...

The script file (.js) isn't showing up on the PHP or HTML webpage

Experiencing a peculiar issue and seeking advice on alternative solutions due to my limited experience in this matter. The Issue: I currently have the following script running smoothly: <script type="text/javascript" id="myscript" src="http://piclau ...

Issue with Vue router: history mode fails to work on the second level

I recently added a second router to my Vue.js project, specifically for a dashboard on my website. However, I encountered an issue where if I manually tried to access a specific route like "/admin/contact" or refreshed the page, I would receive a 404 error ...

Gradual appearance animation upon webpage load

I am attempting to create a fading effect on my homepage that is consistent every time it loads. Unfortunately, the current script I have only seems to work sporadically. The code I am currently using sets the fade effect on the html element since applyi ...

Toggle div visibility with a click

Hello, I've created a mailbox with show/hide functionality on :hover. However, someone recently mentioned that this could be quite distracting for users. Since pure CSS doesn't allow me to achieve the same effect on :active, I'm wondering if ...

Converting JSON data into two HTML tables using jQuery

In this jQuery snippet, JSON data is parsed and an HTML table is created within a div: $(document).ready(function () { const jsonData = $.getJSON("js/quotes.json"); function quotes(a, b) { (this.$div = $(a)), (this._quotes = b), ...

Determine the HTTP request method when capturing AJAX responses

As I take control of all AJAX responses on a global scale, I find myself searching for an elegant solution to identify the method (such as POST/PUT/GET) that was initially used to make the request triggering the intercepted response. Below is my approach ...

Is there a way to compare timestamps in PostgreSQL using moment.js and focus only on the date aspect?

I've encountered a small issue that has me stumped - I'm trying to figure out the best solution for it. The problem lies in a table I have, with attributes named "Start" and "End". My objective is to store every row in an array where the "Start" ...