1

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(); } } 
1
  • hagner's answer is what you need, but may I also suggest using the new HttpClient instead of 'Http'. It has a number of improvements :-) Commented Aug 26, 2017 at 14:58

2 Answers 2

4

You need to make your service injectable:

@Injectable() export class GetWeatherService { //CODE } 
Sign up to request clarification or add additional context in comments.

1 Comment

I thought I was adding Injectable decorator when I want inject dependency to this service from another service. Thank's a lot. It works. Greetings
2

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); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.