I am very new to Angular and trying to play with the real data.
I am making an API call to darksky API , well at least I am trying to do.
What I want to do is ;
At the beginning I have a small form, which asks from user to enter lat
and long
coordinations of a place.
The code is actually working fine, If I pre-define the lat
and long
and give it as parameter to my Link.
I've created a service which makes the API call.
And I couldn't find any possible way to take my forms input value and put it into a service. How can I create it ?
My code ;
My Service app.service.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class WeatherService {
constructor (private http: Http) {}
getWeatherCast() {
// If I pre-define lat and long , it works otherwise it says ' lat and long are not known variables :(
return this.http.get('https://api.darksky.net/forecast/ae249dfb82f6c414cde7809cd2c83e6d/'+ lat + ',' + long)
.map((res: Response ) => res.json());
}
}
My app.component.ts
import {Component, OnInit} from '@angular/core';
import { WeatherService } from "./app.service" ;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
constructor(private weatherService : WeatherService) {}
ngOnInit() { }
currentWeather ;
test (value: any ) {
// console.log(value);
console.log(Number(value.lat), 'lat');
console.log(value.long, 'long');
this.weatherService.getWeatherCast().subscribe(data => this.currentWeather = data);
console.log('Weather is ; ', this.currentWeather);
}
}
and my html ;
<div class="container">
<h3> Test Weather application</h3> <br>
<form #myForm ='ngForm' (ngSubmit)="test(myForm.value)">
<div class="form-group">
<label> Lat </label>
<input type="text" class="form-control" name="lat" ngModel>
</div>
<div class="form-group">
<label> Long </label>
<input type="text" class="form-control" name="long" ngModel>
</div>
<button class="btn btn-primary" type="submit">
<h2> Find out What's the weather like ! </h2>
</button>
</form>
</div>