setting up observable for typing into input

master
Matt Huntington 9 years ago
parent 032595bb65
commit 0e1153d16a

@ -1,7 +1,10 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http'; import { Http } from '@angular/http';
import 'rxjs/add/operator/map'; import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/toPromise';
// Observable operators
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
@Component({ @Component({
selector: 'app-search', selector: 'app-search',
@ -10,25 +13,35 @@ import 'rxjs/add/operator/toPromise';
}) })
export class SearchComponent implements OnInit { export class SearchComponent implements OnInit {
results; results;
searchTerms = new Subject<string>();
constructor( constructor(
private http: Http private http: Http
) { } ) { }
findCharacter(name){ findCharacter(name){
const result = this.http.get('http://swapi.co/api/people/?search=' + name) this.searchTerms.next(name);
.map((response)=>{
return response.json().results; // const result = this.http.get('http://swapi.co/api/people/?search=' + name)
}).subscribe( // .map((response)=>{
(response)=>{ // return response.json().results;
this.results = response; // }).subscribe(
} // (response)=>{
); // this.results = response;
// }
// );
// .toPromise() // .toPromise()
// .then(response => console.log(this.results = response.json().results) ); // .then(response => console.log(this.results = response.json().results) );
} }
ngOnInit() { ngOnInit() {
this.searchTerms
.debounceTime(300)
.distinctUntilChanged()
.subscribe((term)=>{
console.log(term);
});
} }
} }

Loading…
Cancel
Save