Encountered an issue during the migration process from AngularJS to Angular: This particular constructor is not compatible with Angular's Dependency

For days, I've been struggling to figure out why my browser console is showing this error. Here's the full stack trace:

Unhandled Promise rejection: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please ensure that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors. ; Zone: <root> ; Task: Promise.then ; Value: Error: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please ensure that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.
    at ɵɵinvalidFactoryDep (injector_compatibility.ts:94:9)
    at Object.AppModule_Factory [as useFactory] (ɵfac.js? [sm]:1:1)
    at Object.factory (r3_injector.ts:450:32)
    at R3Injector.hydrate (r3_injector.ts:352:29)
    at R3Injector.get (r3_injector.ts:235:23)
    at injectInjectorOnly (injector_compatibility.ts:64:29)
    at ɵɵinject (injector_compatibility.ts:81:58)
    at useValue (provider_collection.ts:249:62)
    at R3Injector.resolveInjectorInitializers (r3_injector.ts:284:9)
    at new NgModuleRef (ng_module_ref.ts:86:22) Error: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please ensure that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.
    at ɵɵinvalidFactoryDep (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:4774:11)
    at Object.AppModule_Factory [as useFactory] (ng:///AppModule/ɵfac.js:4:37)
    at Object.factory (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6968:38)
    at R3Injector.hydrate (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6881:35)
    at R3Injector.get (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6769:33)
    at injectInjectorOnly (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:4758:33)
    at ɵɵinject (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:4762:61)
    at useValue (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6561:65)
    at R3Injector.resolveInjectorInitializers (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6818:17)
    at new NgModuleRef (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:21725:26)

This is what my app.module.ts file looks like:

    import "@angular/compiler";
    import "zone.js";
    import {DoBootstrap, NgModule} from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { UpgradeModule } from '@angular/upgrade/static';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    // import moment from "moment";
    
    @NgModule({
        imports: [
            BrowserModule,
            UpgradeModule
        ],
    })
    export class AppModule implements DoBootstrap {
        constructor(private upgrade: UpgradeModule) { }
        ngDoBootstrap() {
            this.upgrade.bootstrap(document.body, ['CoreApplication'], { strictDi: true });
        }
    }

platformBrowserDynamic().bootstrapModule(AppModule);

I'm utilizing importmap to manage ES modules, and it seems to be working fine. However, upon bootstrapping the application, I encounter the mentioned error. Any assistance would be greatly appreciated.

[EDIT 1]

While attempting to debug the library, I noticed that the error occurs at provider.useFactory(), where the provider object appears as follows:

{
  deps: []
  provide: ƒ AppModule(upgrade)
  useFactory: ƒ AppModule_Factory(t)
  [[Prototype]]: Object
}

The line at https://github.com/angular/angular/blob/211c35358a322f6857c919a2cc80218fbd235649/packages/core/src/di/r3_injector.ts#L450 uses injectArgs to retrieve an array, but in my case, it returns an empty array causing the function to halt. Should I declare a factory somewhere? If so, where should it be declared?

[EDIT 2]

It seems that the function provider.useFactory is defined in this manner:

'function AppModule_Factory(t) {\n  return new (t || jit_AppModule_0)(jit___invalidFactoryDep_1(0));\n}'

Answer №1

Encountered a similar problem and managed to fix it by including an Injection Token in the constructor:

constructor(@Inject(UpgradeModule) private upgrade: UpgradeModule) {}

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

Eliminate redundant template code for Angular 2 components

Currently, I am developing a project using Angular 2 with the user-friendly Gentallela Alela HTML template. In many of my views, there are several components that share similar markup in their template files: <div class="col-md-12 col-sm-12 col-xs-12"& ...

Error: Angular variable is undefined when using template-driven validation

When I create a form using a template-driven approach and submit it, an error occurs stating that all form fields are undefined. The form consists of five fields, and I attempt to clear them using the reset form function. As a novice in Angular, I am uncer ...

determine the appropriate month for the calendar month component based on the route selected

I have developed a calendar component where I want to preselect the default month based on the route parameters received for the component. Here is the calendar: <p-calendar [maxDate]="dateTime" [(ngModel)]="selectedMonth" name=&quo ...

Create an interactive Angular form that dynamically generates groups of form elements based on data pulled from

I am currently developing an Angular application and working on creating a dynamic form using Angular. In this project, I am attempting to divide the form into two sections: Person Name and Personal Details. While I have successfully grouped fields for P ...

What is the best way to extract data from a proxy in VUE3?

Currently, I am utilizing the ref() function to store data retrieved from Firebase. However, when attempting to filter and retrieve a single record, the outcome is not as expected. Instead of returning a single object, something different is being displaye ...

Utilize AJAX within an Angular method

I am encountering 2 issues with the $http function in this particular code snippet. $scope.buttonClick = function(){ // Sending input data $.post('lib/add_contact.php', $scope.contact, function(data){ data = angular.fromJ ...

Endless spirals within the confines of an angular controller

Every time I launch my app in a window, it gets caught in an endless loop where angular continuously calls the GameCtrl and ultimately freezes the window. Here is the code snippet causing the issue: index.html <!DOCTYPE html> <html ng-app="baseba ...

When tests/** are not included in the tsconfig, the TS language features in Vscode become inaccessible

I am looking to configure my TypeScript tests in such a way that they receive linting, code completion, and VSCode intellisense (TypeScript language features) when the test folder is placed next to the src folder. However, I want to ensure that my tests do ...

Creating a personalized fake database functionality in Angular

Is there a way to implement the fake-db feature in Angular while utilizing this resource? I need it to support an API response structure like the one below for list retrieval. // GET 'api/outlets' { data: [ {'id':1, 'name&ap ...

angularjs $http service fails to create a cookie when sending a POST request but successfully creates one when sending a GET request

For my project, I have set up the server side to provide WebApi 2.2 while the client side is a mobile application built with Ionic. Currently, everything is running locally in Chrome with the mobile app in emulation mode. 1) To enable CORS on the server, ...

Angular.js dynamically changing ng-class based on long-polling updates to a monitored variable

Currently utilizing angular.js for my project. I am implementing long polling with a server and would like to dynamically update an element in the view, specifically one with a class of 'updated', whenever the value of build_tag is incremented. ...

Connect data from an HTML table depending on the chosen option in a dropdown menu using AngularJS, JQuery, JSON, and

Could you please correct my errors? It's not working as I have made some mistakes. I need an HTML table based on the selection. I have tried but cannot find a solution. I created a dropdown, and if I select any value from the dropdown and click a butt ...

The Angular-Chart.js chart fails to display when data is automatically inserted

I came across this sample code at https://stackblitz.com/edit/angular-chartjs-multiple-charts and tried using it. Everything was working well with static data, but when I attempted to push data retrieved from the Firebase Realtime Database into the chart, ...

Click function for mat-step event handler

Is it feasible to create a click event for the mat-step button? I want to be able to add a (click) event for each mat-step button that triggers a method. Essentially, I am looking to make the mat-step button function like a regular button. You can find mo ...

What is the best way to access the "then()" method in AngularJS?

Here is the scenario I am dealing with: app.js: let app = angular.module('myApp', []); app.controller('login', function ($scope, $login) { $scope.account = {}; $scope.loginForm_submit = function ($event, account) { $ ...

Replace Material UI propTypes with new definitions

I am currently working with React, Typescript, and Material UI. To globally disable the ripple effect for the ListItem component, I am using createMuiTheme.props in the following manner: createMuiTheme({ props: { MuiListItem: { disableRipple: t ...

Problem with TypeScript involving parameter destructuring and null coalescing

When using parameter destructuring with null coalescing in TypeScript, there seems to be an issue with the optional name attribute. I prefer not to modify the original code like this: const name = data?.resource?.name ?? [] just to appease TypeScript. How ...

Trigger the ngOnInit() function of the app component for a second time by clicking on a link

Currently, I am in the process of restructuring an Angular project and came across the following functionality. Inside app.component.ts file ngOnInit() { this.portfolioID = Number(sessionStorage.getItem('portfolioID')); console.log(this.portfol ...

In TypeScript, values other than numbers or strings can be accepted as parameters, even when the expected type is a

The issue I am encountering with TypeScript is quite perplexing, especially since I am new to this language and framework. Coming from a Java background, I have never faced such a problem before and it's presenting challenges in my bug-fixing efforts ...

Changing the Date Format in AngularJS: A Step-by-Step Guide

I have a date coming from the database in the format "2015-09-21 18:30:00". I need to change it to 'dd/MM/yyyy'. I attempted it like this {{obj.added_date | date : 'dd/MM/yyyy'}} It displays as: 2015-09-21 18:30:00 | date : 'dd/ ...