in Education by
Trying to search trough extrnal api Weather servis. Api is tested and works fine. Problem: Enter value in search bar and main Search method from servis is not called. That is reason why request is not sent. I follow tutorial from angular official documentation: https://angular.io/tutorial/toh-pt6#search-by-name And this not work. Search Component: export class SearchCityComponent implements OnInit { cities$: Observable private searchTerms = new Subject() constructor(private weaterService: WeatherService) {} // Push a search term into the observable stream. search(term: string): void { this.searchTerms.next(term) } ngOnInit(): void { this.cities$ = this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.weaterService.searchCities(term)) ) } } Service: /* GET cities whose name contains search term */ searchCities(term: string): Observable { if (!term.trim()) { return of([]) } return this.http .get(`${this.citiesUrl}/?query=${term}`, httpOptions) .pipe( tap(_ => this.log(`found cities matching "${term}"`)), catchError(this.handleError("searchCities", [])) ) } When I enter in search input some value: In Search Comnponent in ngOnInit() last line of code is Not called search function from services? switchMap((term: string) => this.weaterService.searchCities(term)) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
An observable will be triggered only if it's needed, to tell to an Observable that you need it you have to subscribe to it : this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.weaterService.searchCities(term)) ).subscribe()

Related questions

0 votes
    Trying to search trough extrnal api Weather servis. Api is tested and works fine. Problem: Enter value in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    what i want : i have a config file where it contains some urls in .json file stored in asset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    How do I redirect to another route without a hard refresh? I created an ionic / angular app for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    it is possible to crawl a website within an Angular-App? I am speaking about to call a website ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    it is possible to crawl a website within an Angular-App? I am speaking about to call a website ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have used angular-cli with systemJS and am now comfortable with its build process,test cases & component ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    How does an Angular application work?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I made a JQuery function the one deletes the row I selected. I know that the option exist in the DataTables option, but I want to ... }*/ });}); Then, I have the JSON file there:...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I made a JQuery function the one deletes the row I selected. I know that the option exist in the DataTables option, but I want to ... }*/ });}); Then, I have the JSON file there:...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    How does one share data between components in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    I'm not sure why the change detection wouldn't work here. I've tried a few things, including a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I'm not sure why the change detection wouldn't work here. I've tried a few things, including a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I am trying to create a windows service. The purpose of service is to pick up urls from a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Which of the following is a private Search-engine and do not track our searching data? (a) Google (b) ... Need-for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
...