I have two components, SearchComponent and RuleListComponent. The Search component is a child of RuleList.
https://i.stack.imgur.com/rFlM2.png
I need the SearchComponent to retrieve data using APIService. This data should then be passed to RuleList as an observable.
SearchComponent:
export class SearchComponent implements OnInit {
items: Observable<Array<string>>;
term = new FormControl(); # term is asynchronous
constructor(private apiService: APIService) { }
ngOnInit() {
this.items = this.term.valueChanges
.debounceTime(1000)
.distinctUntilChanged()
.switchMap(term => this.apiService.fetch('rule', term));
}
}
APIService:
@Injectable()
export class APIService {
baseUrl: string;
constructor(private http: Http) {
this.baseUrl = '//127.0.0.1:8000/';
}
fetch(term: string) : Observable<any> {
console.log('searching');
var search = new URLSearchParams();
search.set('search', term);
return this.http.get(this.baseUrl, { search })
.map(response => response.json());
}
}
Is there a way to continuously pass data from SearchComponent to RuleListComponent based on changing terms asynchronously?