Reputation: 159
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
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
Reputation: 1050
You need to make your service injectable:
@Injectable()
export class GetWeatherService {
//CODE
}
Upvotes: 4