Unable to instantiate an Angular component constructor using a string parameter

So, I've created a simple component:

export class PlaintextComponent implements OnInit {
  schema: PlaintextTagSchema;

  constructor(private _ngZone: NgZone, prompt: string, maxRows: number, maxChars: number) {
    this.schema.prompt = prompt;
    this.schema.maxRows = maxRows;
    this.schema.maxChars = maxChars;
  }

  ngOnInit() {
  }
}

But when attempting to compile my app using ng serve, I encountered an error:

component.ts:25:40 - error NG2003: No suitable injection token for parameter 'prompt' of class 'PlaintextComponent'.
Found string

25   constructor(private _ngZone: NgZone, prompt: string, maxRows: number, maxChars: number) {

I have scoured the internet for solutions to this puzzling error, but so far, nothing has helped me resolve it. Despite my best efforts, the constructor setup appears correct to me. However, as a newcomer to Angular/TypeScript, I may be overlooking a fundamental detail. Any assistance would be greatly appreciated. Thank you.

Answer №1

Revamp the Constructor as shown below

  constructor(private _ngZone: NgZone, @Inject(String) alert: string, numTabs: number, maxWords: number)

remember

import { Inject } from '@angular/core';

Answer №2

Encountered the same issue myself. It seems like Angular is attempting dependency injection with the constructor parameters (similar to how it's done with services), which may cause errors in certain cases. To resolve this, simply move your class properties outside of the constructor declaration. Here's an example:

export class PlaintextComponent implements OnInit {
  schema: PlaintextTagSchema;
  private _ngZone: NgZone;
  prompt: string;
  maxRows: number; 
  maxChars: number;

  constructor() {}

  ngOnInit() {
  }
}

You may need to find an alternative method to initialize your Component, such as using input properties.

If you give this a try, I'd love to hear if it resolves the issue! ;D

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

How can one use an Angular Route to navigate to a distinct URL? Essentially, how does one disable matching in the process?

I'm working on a front-end Angular application and I need to add a menu item that links to an external website. For example, let's say my current website has this URL: And I want the menu item in my app to lead to a completely different website ...

Building a Next.js application in a docker container on a local machine using minikube is successful, however, the build fails when attempting to build it on a staging environment

I've encountered a puzzling issue. My next.js app is running in a docker container. It builds successfully on my local Ubuntu machine with minikube and ks8 orchestration, but it gets stuck indefinitely during the build process on the staging setup. T ...

Gain access to JSON information and store it in the datasource variable using the Kendo datasource function

Within my asp.net project, I have utilized the following approach to extract Json data from a js file: var ds = new kendo.data.DataSource({ transport: { read: { url: "http://localhost:2544/JS/emp ...

Performing JavaScript functions after fetching an XSLT page via AJAX

Is there a way to trigger JavaScript at a particular time without relying on setInterval() or onClick()? The issue I'm facing is that when I click to load an XSLT page using Ajax, the JavaScript within it does not execute even though it's writt ...

A comparison of parent and child components

To implement a child-parent component relationship in Angular, first create two JSON files: parent.json and child.json. The parent.json file should contain the following data: "Id":10001, "name":"John" "Id":10002, ...

Guide on implementing iterative data path in v-for loop using Vue

I'm just starting out with Vue and I want to use image file names like "room1.jpg", "room2.jpg", "room3.jpg" in a loop Below is my code, where the second line seems to be causing an issue <div v-for="(p,i) in products" :key="i"> <img src ...

Angular select automatically saves the selected option when navigating between views

I would like the selected option in my dropdown menu to stay selected as I navigate through different views and then return back. Here is the view: <select ng-model="selectedSeason" class="form-control" ng-options="season as 'Season '+ seas ...

How to iterate through a "for" loop in JavaScript using Python-Selenium?

In my current project, I am utilizing Javascript to gather data from an HTML page using Selenium. However, I am facing a challenge where I am unable to execute the multi-line for loop in the Javascript portion on my computer through Selenium with Python (S ...

The redesigned pie chart animations in d3 are experiencing glitches

Previously, I had a pie chart with fantastic animations. You can view it here: https://jsfiddle.net/tk5xog0g/29/ I tried to rebuild the chart and improve it based on my needs, but the animations no longer work as intended. I suspect this might be due to ...

Updating the state of Formik

Currently, I'm knee-deep in a React project that requires a slew of calculations. To manage my forms, I've turned to Formik, and for extra utility functions, I've enlisted the help of lodash. Here's a peek at a snippet of my code: impor ...

Ionic 3 connection with WooCommerce store facing issues due to absent parameters

This is the code snippet I am working with: signup(){ let customerData = { customer : {} } customerData.customer = { "email": this.newUser.email, "first_name": this.newUser.first_name, "last_name": this ...

Discover and showcase individual product information using Product ID in Angular 8 with Firebase integration

I've been struggling to retrieve a single product record from Firebase using Angular 8. I'm new to this and have spent the past two days trying to figure it out. Below is my Firebase database. I have all products displayed on one page, and when c ...

Revamp State before redirecting using React Router Dom

In my current project, I am facing an issue with redirecting after updating the state successfully. The technologies I'm using include React 16.12.0 and React Router DOM 5.1.2. Here's the scenario: I have a button that, when clicked, should updat ...

Is jQuery AJAX returning improperly formatted JSON when using the jsonp type?

$('#rn_s').keyup(function() { var rn = $('#rn_s').val(); if(rn.length == 9) { $.ajax({ url: 'http://routingnumbers.info/api/data.json?rn=' + rn, type: 'GET', dataT ...

Having trouble getting rid of the border-bottom?

I have been attempting to customize the appearance of the React Material UI tabs in order to achieve a design similar to this: https://i.stack.imgur.com/tBS1K.png My efforts involved setting box-shadow for the selected tab and removing the bottom border. ...

Heroku-hosted application experiencing issues with loading website content

I have been working on a nodejs application that serves a web page using express and also functions as a discord bot. The app runs smoothly on localhost with both the web page and discord functionalities working correctly. However, when I deploy it to Hero ...

Exploring the process of extracting a nested JSON value using Postman

I am currently facing an issue with parsing the json response from a post request, and then sending the parsed data to a put request. Here is the response body: { "createdBy": "student", "createdOn": "2019-06-18", "Id1": "0e8b9 ...

Sending JSON data from a Django view to a JavaScript function

I am trying to pass JSON data to JavaScript. I need the JSON structure to be like this: data: [ { value: 335, name: 'Coding' }, { value: 310, name: 'Database ...

What are the best practices for integrating PrimeVue with CustomElements?

Recently, I decided to incorporate PrimeVue and PrimeFlex into my Vue 3 custom Element. To achieve this, I created a Component using the .ce.vue extension for the sfc mode and utilized defineCustomElement along with customElements.define to compile it as a ...

When converting a PDF to a PNG, the precious data often disappears in the process

I am currently facing a problem with the conversion of PDF to PNG images for my application. I am utilizing the pdfjs-dist library and NodeCanvasFactory functionality, but encountering data loss post-conversion. class NodeCanvasFactory { create(w, h) { ...