There is a input text that for each change, he get from my service the search results. My serivce returns : Observable that contain all the results. It will return multiple persons.
tamplate.html:
<input type="text" id="inputSearch">
...
<tr *ngFor="let person of items$ | async" ...
...
Code:
import { Component ,OnInit} from '@angular/core';
import {Control} from '@angular/common';
import {PersonService} from "./person.service";
import {Person} from "./person.class";
import {Observable} from "rxjs/Rx"
@Component({
selector: 'contact-table',
moduleId: module.id,
templateUrl: 'contact-table.component.html',
styleUrls: ['contact-table.component.css']
})
export class ContactTable implements OnInit {
private items$: Observable<Person[]>;
private inputChanged$:Observable<{}>;
constructor(private _PersonService:PersonService) {}
public ngOnInit() {
this.inputChanged$=Observable.fromEvent(document.getElementById("inputSearch"), 'input');
this.items$= this.inputChanged$
.map((event:any)=>event.target.value)
.switchMap((value:string) =>{
return this._PersonService.getPersons(); <<-- it gets here!****
})
.toArray();
this.items$.subscribe((persons:Person[])=>{
console.log(persons); <<-- it doesn't gets here!****
});
}
}
Update1:
By Günter Zöchbauer tip, I added menual subscription in the last line of the method but that code is never excuted and nothing gets logged. Any idea why?
Update2: I added the full ts file of my code.
Updated3:
Solved - But don't know why: ( I moved toArray() )
public ngOnInit() {
this.inputChanged$=Observable.fromEvent(document.getElementById("inputSearch"), 'input');
this.items$= this.inputChanged$
.map((event:any)=>event.target.value)
.switchMap((value:string) =>{
return this._PersonService.getPersons().toArray(); <<-- moved here
}); <<-insead of here.
this.items$.subscribe((persons:Person[])=>{
console.log(persons); <<-- it doesn't gets here!****
});
Any idea why it made any diffrence?