I've been experimenting with the hero app tutorial for Angular 2 and currently have this Component set up:
import { Component, OnInit } from '@angular/core'
import { Subject } from 'rxjs/Subject';
import { Hero } from "./hero";
import { Router } from "@angular/router";
import { HeroService } from "./hero.service";
import { BehaviorSubject } from "rxjs/BehaviorSubject";
@Component({
selector: 'hero-search',
templateUrl: 'app/hero-search.component.html',
styleUrls: ['app/hero-search.component.css'],
})
export class HeroSearchComponent implements OnInit{
heroes: Hero[];
isLoading: BehaviorSubject<boolean> = new BehaviorSubject(false);
error: any;
private searchNameStream = new Subject<string>();
constructor(
private heroService: HeroService,
private router: Router
) {}
ngOnInit() {
this.searchNameStream
.debounceTime(400)
.distinctUntilChanged()
.switchMap(name => {
this.isLoading.next(true);
return this.heroService.getHeroesByName(name)
})
.subscribe(
heroes => this.heroes = heroes,
error => this.error = error,
() => {
console.log('completed');
this.isLoading.next(false);
})
}
// Push a search term into the observable stream.
search(Name: string): void {
this.searchNameStream.next(Name)
}
gotoDetail(hero: Hero): void {
let link = ['/detail', hero.id];
this.router.navigate(link);
}
}
The issue I'm encountering is that, according to my understanding, the subscribe method takes three callback parameters
.subscribe(success, failure, complete);
. However, in my case, the complete part is never executed. Could this be related to how switchMap functions? Am I on the right track?