50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
import { Component } from '@angular/core';
|
|
import { Beer } from './beer';
|
|
import { BeerService } from './beer.service';
|
|
import { OnInit } from '@angular/core';
|
|
|
|
import { ActivatedRoute, Params } from '@angular/router';
|
|
|
|
import { Router } from '@angular/router'
|
|
|
|
import 'rxjs/add/operator/switchMap';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'my-beer-search',
|
|
templateUrl: './beer-search.component.html'
|
|
})
|
|
|
|
export class BeerSearchComponent implements OnInit {
|
|
beers: Beer[];
|
|
selectedBeer: Beer;
|
|
search: string;
|
|
|
|
constructor(
|
|
private router: Router,
|
|
private beerService: BeerService,
|
|
private route: ActivatedRoute) { }
|
|
|
|
getBeers(): void {
|
|
this.route.params
|
|
.switchMap((params: Params) => this.beerService.search(params['id']))
|
|
.subscribe(b => this.beers = b);
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.getBeers();
|
|
}
|
|
|
|
onSelect(beer: Beer): void {
|
|
this.selectedBeer = beer;
|
|
}
|
|
|
|
gotoDetail(): void {
|
|
this.router.navigate(['/detail', this.selectedBeer.id]);
|
|
}
|
|
|
|
searchBeer(): void {
|
|
this.router.navigate(['/search', this.search]);
|
|
}
|
|
}
|