I'm having trouble wrapping my head around implementing an Observable clicking event to a function I already implemented. The examples I have seen so far are implemented using the fromEvent or Subject like so;
import {Subject} from "rxjs/Subject";
export class Component {
private clickStream$ = new Subject<Event>();
@Output() observable$ = this.clickStream.asObservable();
buttonClick(event:Event) {
this.clickStream$.next(event);
}
}
their html
<button type="button" (click)="buttonClick($event)">click me</button>
Now consider my implementation below.
component.ts
export class MyComponent {
result;
public search(queryString: string) {
this.myService.search().subscribe((res) => result = res )
}
}
index.html
<form [formGroup]="searchForm">
<input
formControlName="queryString"
placeholder="Enter keyword"
type="text"
(keyup.enter)="search(searchForm.value.queryString)"
>
<div (click)="search(searchForm.value.queryString)"></div> <!-- added an icon with css ->
</form>
service.ts
export class SearchService {
constructor(private http: HttpClient) {}
search(queryString: string) {
if (!queryString) {
return;
}
return this.http.get(`${this.BASE_URL}&q=${queryString}`);
}
}
My trouble is how to use my current search() and still be able to subscribe to my myService.search() method while using the approach I found. (i.e. the example code in this post)
I understand ( I think ) streams, and the concept of Observables, but yet this is kicking my butt.
thanks in advance for any help.
onSearchis not the same name assearchand should throw en exception. 2. you can't return anything useful from subscribe. You can make an assignment butreturn resis useless. If you wanted to see the latest value you could create a field in your component and assign to that ore use an async pipe and return an observable from the method (or assign the resulting observable to a field in your component.). 3. Your component methodsearchreceives an input but you do nothing with that in your service call, why? - Igor@Output()usage looks wrong. Here is@Outputexplained angular.io/guide/template-syntax#how-to-use-output - sneas