0

Hie guys i am trying to implement search feature using angular2 http request with the help of this link http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html ....In this example the response from wiki will be an array of string so the code will be like this...

@Component({
selector: 'my-app',
 template: `
<div>
  <h2>Wikipedia Search</h2>
  <input type="text" [ngFormControl]="term"/>
  <ul>
    <li *ngFor="#item of items | async"></li>
  </ul>
</div>
 `
 })
export class App {
items: Observable<Array<string>>;
 term = new Control();
 constructor(private wikipediaService: WikipediaService) {
this.items = wikipediaService.search(this.term.valueChanges);
}
}

If the response is an array of objects then i am changing this statement from

items: Observable<Array<string>>; to items: Observable<Array<object>>;

but i am not getting it...somebody please show me a way to implement if the response from remote server is an array of object ... Here is my demo http://plnkr.co/edit/BE3l7lQpic9rzDZyomhP?p=preview Assuming the response is array of objects please guide me

1 Answer 1

2

You forgot to include the operators you use. You can add this in your imports:

import {Injectable} from 'angular2/core';
import {URLSearchParams, Jsonp} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/debounceTime'; // <----

@Injectable()
export class WikipediaService {
  (...)
}

See this plunkr: http://plnkr.co/edit/ybShAY1hOLoGG2WHlHTW?p=preview.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Thierry but when the response is Array of objects then what changes will you make in your component and html file? Can u please update the plnkr assuming u get response something like this [ object1,object2,object3 ....]
You're welcome! Here is a plunkr describing this: plnkr.co/edit/OZXlXL7DnB15alaCXCAm?p=info. Is it what you need?
Yea Thierry Templier...Can i use the same code to implement when user types something so that i can show some value returned in a list?
Because i need to implement it using Debounce Time and distinctUntilChanged
Sure you can ;-) Have a look at this plunkr: plnkr.co/edit/MhDl8hWjwhAlaUw2i8hy?p=preview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.