You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB

import { Component, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { SearchService } from './search.service';
// Observable class extensions
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css'],
providers: [SearchService]
})
export class SearchComponent implements OnInit {
results;
searchTerms = new Subject<string>();
constructor(
private characterSearchService: SearchService,
) { }
findCharacter(name){
this.searchTerms.next(name);
}
ngOnInit() {
this.searchTerms
.debounceTime(300)
.distinctUntilChanged()
.subscribe(name => {
this.characterSearchService.createAPIObservable(name).subscribe(results => {
this.results = results.json().results;
})
})
}
}