Encountering an issue in Angular 2 Typescript where not all parameters can be resolved for TooltipService

After injecting a service that makes an http.get request into a component, I encountered an error. The component rendered fine initially, but the service injection triggered an exception.

The error message states: EXCEPTION: Can't resolve all parameters for TooltipService: (?).

To troubleshoot, I included bootstrap as I suspect it may be a DI issue.

import { Component, Input } from '@angular/core';
import { TooltipService } from '../../services/tooltip/tooltip.service';

@Component({
    ..,
    ..,
    providers: [TooltipService]
})

export class TooltipComponent implements OnInit {
    @Input() explaination: boolean;
    @Input() nodeId: number;
    @Input() questionId: number;

    constructor(public tooltipService: TooltipComponent) {}

    ngOnInit() {
        if(this.explaination) {
            this.tooltipService.getExplaination(this.nodeId, this.questionId)
                .then(response => console.log(response));
        }
    }
}

-

import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/toPromise';

@Injectable()

export class TooltipService {
    constructor(public http: HTTP) {}

    getExplaination(private nodeId: number, private questionId: number) {

        let url = `someUrl/` + questionId + `/` + nodeId;

        return this.http.get(url)
            .toPromise()
            .then(response => response.json())
            .catch(this.handleError);
    }

}

-

import { bootstrap } from '@angular/platform-browser-dynamic';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
import { HTTP_PROVIDERS } from '@angular/http';

import {AppComponent} from '../app/components/app/app.component';

bootstrap(AppComponent, [
        HTTP_PROVIDERS,
        disableDeprecatedForms(),
        provideForms()
    ])
    .catch((err: any) => console.error(err));

Answer №1

Your code contains 2 typos that need to be corrected.

Instead of using HTTP, it should be written as Http:

export class TooltipService {
    constructor(public http: Http) {}

It seems like you should be injecting the service, not the component:

export class TooltipComponent implements OnInit {
    @Input() explanation: boolean;
    @Input() nodeId: number;
    @Input() questionId: number;

    constructor(public tooltipService: TooltipService) {}

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

Material UI Grid has a problem with inconsistent column width when using direction='column' and flexWrap='wrap' attribute combination

I'm a newcomer to React and Frontend development in general. Currently, I am working on setting up a MUI Grid that organizes items in columns which wrap around when necessary. So far, I've been able to accomplish this by adjusting the direction a ...

Determine the associated value for a given key within a TypeScript object

I have a structure like this: type newsItem = { img: string; slug: newsSlug; text: newsText; }; derived from an enum like this: export const newsEnum = { interesting: "Interesting", regions: "Regions", contradictory: " ...

Display a message to a user on the same HTML page using jQuery or JavaScript

How can I show a message to the user on the HTML page confirming their selected date without using an alert box? I attempted this code snippet, but I'm uncertain: document.writeln("The date you picked is: " + dateText); ...

Experiencing complications with ng-model specifically on the select tag, while encountering no difficulties elsewhere

I am experiencing an issue with my ng-model statements, where all of them are working except for the one on the select tag. To provide context, imagine a page displaying a list of devices, and when one is clicked, a specific function is executed: /* Opens ...

Troubleshooting the malfunction of the Angular 2 Tour of Heroes project following the separation of the app

Recently, I encountered a challenge while following a tutorial on learning Angular 2. Everything was going smoothly until I reached the point where I had to divide appcomponent into heroescomponent & appcomponent. Is there anyone else who has faced th ...

`trivial javascript problem`

In attempting to use an onclick event on another section of a webpage, I encountered difficulties with setting a checkbox to be checked on my web form. Even though all the form and checkbox IDs/names were correctly configured, none of the following metho ...

What is the correct way to inspect the HTTP request body and reject it if it does not adhere to the specified pattern when working with a basic NodeJS server?

I am currently facing a challenge while developing a server using solely nodeJS libraries and methods. I am struggling to validate the request body format and promptly respond with a client error status code type (400-499). Below is the JavaScript code sn ...

Is there an Angular Carousel feature that previews the next image?

Here's the design I'm working on: https://i.sstatic.net/mDroM.png The image is centered with a glimpse of the previous/next images on the sides. I've explored various angular carousels from but none seem to have this specific feature. C ...

Utilize numerous instances of redux-form on a single webpage and submit them all at once with a

Currently, I am utilizing redux-form within a Modal that contains 'Validate' and 'Cancel' buttons. I have three distinct forms, each of which will generate three separate entities in the backend. My aim is to produce and dispatch two ...

What could be the reason for this JavaScript malfunctioning in both Chrome and Safari browsers?

Why is it that the javascript works fine in Firefox for the gallery on the page below, but doesn't seem to be functioning properly in Chrome and Safari? In Chrome, the big image isn't displaying but the thumbnails are, and in Safari nothing show ...

XState: linking together multiple promises seamlessly without needing intermediate states

After reading the Invoking Multiple Services section, it seems that despite being able to invoke multiple promises, they appear to be triggered without waiting for the previous one to complete in my own tests. // ... invoke: [ { id: 'service1' ...

What is the best approach to iterate through multiple input fields while maintaining separate states for each one?

Utilizing Vuetify to create text fields and applying v-model to a textFieldState results in all text fields sharing the same state, causing input from one field to leak into others. How can I ensure that each field maintains its own state? <div v- ...

When using the setTimeout function, jQuery's .submit() method may fail to pass data to PHP

I'm attempting to enhance the login form with an effect that triggers a 1-second delay before proceeding to PHP. I tried implementing a setTimeout() function within the .submit event handler along with e.preventDefault() to create the delay, but enco ...

Having issues with Google Maps API v3 not loading properly

I'm encountering an issue with initializing a map upon window load. While the problem is similar to this question, I am not utilizing jQuery to create the map, rendering the solution provided there inapplicable to my situation. Here's my code sni ...

Tips on utilizing imacros for extracting image URLs

I am currently experimenting with the use of imacross for web scraping, but I have encountered a roadblock when it comes to extracting image URLs from markdown code like the one provided below. <div class="dpimages-icons-box"> <a href="http: ...

Displaying the getJSON output only once, then having it automatically update at regular intervals without any repetitive results

I previously had an issue resolved on stackoverflow, but the requirements of my project have changed. Therefore, I am in need of a new solution. In summary, I have a getJSON function that runs every 5 seconds to check for changes in a JSON file. The proble ...

What is the proper way to invoke a variable-function in Node.js?

I have a function called `findPeopleByName` that queries the database for a specific username, but I'm not sure how to call it in my Node.js code. Here's what I have so far: // Retrieve user_name from POST request. app.post("/api/exercise/new-u ...

Undefined variable when initializing with ng-init

As a newcomer to AngularJS (and JavaScript in general), I'm currently facing an issue that I could use some help with. Below is the HTML template I am using: <ion-view view-title="Playlists"> <ion-content> <ion-list> ...

JSON has encountered an unconventional string at position 41 that was not anticipated

Attempting to learn Javascript as a beginner, I am facing an error that is puzzling me: SyntaxError: /home/runner/myrepl/config.json: Unexpected string in JSON at position 41 Upon checking my index.js file, it seems correct at position 41 (at least I beli ...

Encountering a problem with bidirectional binding in Angular select menus

Currently, I am working on creating a multiple dropdown using the ng-select plugin. The specific scenario I'm facing is that when a user selects items from the dropdown, I want to display the selected item object in a button with a close icon directly ...