Hubert
Hubert

Reputation: 159

Angular Uncaught Error: Can't resolve all parameters for service

I don't understand why this service doesn't work? Is it some problem with dependencies?

service:

import { Http } from '@angular/http';

export class GetWeatherService {

    constructor(private http: Http) {}

    getWeather() {
        return this.http.get(link);
    }
}

and component:

import { Component, OnInit } from '@angular/core';
import { GetWeatherService } from './get-weather.service';

@Component({
    selector: 'app-form',
    templateUrl: './form.component.html',
    styleUrls: ['./form.component.css'],
    providers: [GetWeatherService]
})
export class FormComponent implements OnInit {

    constructor(public getWeather: GetWeatherService) {}

    ngOnInit() {
        this.getWeather.getWeather();
    }
}

Upvotes: 1

Views: 606

Answers (2)

Suren Srapyan
Suren Srapyan

Reputation: 68635

Add @Injectable() over your service to let it be injected into constructors.

import { Http } from '@angular/http';
import { Injectable } from '@angular/core';

@Injectable()
export class GetWeatherService {

    constructor(private http: Http) {}

    getWeather() {
        return this.http.get(link);
    }
}

Upvotes: 2

hagner
hagner

Reputation: 1050

You need to make your service injectable:

@Injectable()
export class GetWeatherService {
    //CODE
}

Upvotes: 4

Related Questions