Skip to content

Commit

Permalink
add rxjs
Browse files Browse the repository at this point in the history
  • Loading branch information
NikosDev committed Apr 2, 2018
1 parent 0c99e5e commit 8fe633f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<h2>wikipedia search</h2>
<input (input)="search($event.target.value)">
<input (input)="term$.next($event.target.value)">
<ul>
<li *ngFor="let item of items; index as i">
<a href="{{links[i]}}">{{item}}</a>
Expand Down
18 changes: 16 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Component } from '@angular/core';

import { WikipediaSearchService } from "./wikipedia-search.service";
import { Subject } from "rxjs/Subject";

import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';

@Component({
selector: 'app-root',
Expand All @@ -10,8 +14,18 @@ import { WikipediaSearchService } from "./wikipedia-search.service";
export class AppComponent {
items:Array<string>;
links:Array<string>;
term$ = new Subject<string>();

constructor(private service:WikipediaSearchService){}
constructor(private service:WikipediaSearchService){
this.term$
.debounceTime(400)
.distinctUntilChanged()
.switchMap(term => this.service.search(term))
.subscribe(results => [
this.items= results[1],
this.links= results[3]
]);
}

search(term: string){
this.service.search(term).subscribe(results => [
Expand Down
2 changes: 1 addition & 1 deletion src/app/wikipedia-search.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders, HttpParams } from '@angular/common/http';
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/map';

@Injectable()
export class WikipediaSearchService {
Expand Down

0 comments on commit 8fe633f

Please sign in to comment.