I'm trying to load a list on screen in my browser using Observable, however I'm not having success, this is a record that exists in my url;
This is the service class
import { Http } from '@angular/http';
import { environment } from './../../environments/environment';
import { Restaurant } from './restaurant.model';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class RestaurantService {
public url: String = 'http://localhost:3000/api/';
constructor(private http: Http) {
// this.url = environment.url;
}
restaurants(): Observable<Restaurant[]> {
return this.http.get('${this.url}/restaurants')
.map(response => response.json())
}
I performed a test on the component class using console.log as you can see below;
export class RestaurantsComponent implements OnInit {
restaurants: Restaurant[]
constructor(private restaurantService: RestaurantService){}
ngOnInit() {
this.restaurantService.restaurants()
.subscribe(restaurants => {
console.log('o valor é ' , this.restaurants = this.restaurants);
})
}
And the result is this;
o valor é undefined
What am I doing wrong?