I have a list of restaurants and when clicking on one of them I want the EventEmitter to take the restaurant component information and supply the details component with it. I am trying with the following code. When I'm running getRestaurantDetails() the console log prints out the name of the restaurant but I think it might be not emmiting or being able to subcribe to the emitter. In the details.ts the first console log is triggered and the second and the third one are not. Please help me out. I've trying to figure it out all day.
restaurant.ts
import { Component, Input } from '@angular/core';
import {Restaurant} from "./restaurant.model";
import {RestaurantService} from "./restaurant.service";
@Component({
selector: 'app-restaurant',
templateUrl: './restaurant.component.html',
styleUrls: ['./restaurant.component.css']
})
export class RestaurantComponent {
@Input() restaurant: Restaurant;
constructor(private restaurantService: RestaurantService){
}
onSelect(){
this.restaurantService.getRestaurantDetails(this.restaurant);
}
}
service.ts
import {Restaurant} from "./restaurant.model";
import {Http,Response } from "@angular/http";
import {Injectable,EventEmitter, Output} from "@angular/core"
import 'rxjs/Rx';
import {Observable} from "rxjs";
@Injectable()
export class RestaurantService{
private restaurants: Restaurant[]=[];
@Output() restaurantIsSelected = new EventEmitter<Restaurant>();
constructor(private http: Http){
}
getRestaurantDetails(restaurant: Restaurant){
this.restaurantIsSelected.emit(restaurant);
console.log(restaurant.name);
}
getRestaurantEmitter() {
return this.restaurantIsSelected;
}
}
details.ts
import { Component, OnInit } from '@angular/core';
import {Restaurant} from "./restaurant.model";
import {RestaurantService} from "./restaurant.service";
@Component({
selector: 'app-restaurant-details',
templateUrl: './restaurant-details.component.html',
styleUrls: ['./restaurant-details.component.css']
})
export class RestaurantDetailsComponent implements OnInit{
restaurant: Restaurant;
constructor(private restaurantService: RestaurantService){}
ngOnInit(){
console.log("lol");
this.restaurantService.getRestaurantEmitter().subscribe(
(restaurant: Restaurant)=>{
console.log(restaurant);
console.log("lol");
this.restaurant=restaurant;
});
}
}