Search throug server data does not work in Angular Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How does JavaScript .prototype work?How does the “this” keyword work?how does Array.prototype.slice.call() work?Why does JavaScript only work after opening developer tools in IE once?How does data binding work in AngularJS?How does Access-Control-Allow-Origin header work?How do I pass data to Angular routed components?Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'How to merge 2 searches in Angular Heroes tutorial?How is it working this Angular pipe used to perform a dynamic search of terms inserted into an input tag in my view?
France's Public Holidays' Puzzle
How did Elite on the NES work?
What is /etc/mtab in Linux?
Was there ever a LEGO store in Miami International Airport?
Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table
Bright yellow or light yellow?
How do I deal with an erroneously large refund?
Marquee sign letters
Is a self contained air-bullet cartridge feasible?
Coin Game with infinite paradox
Did war bonds have better investment alternatives during WWII?
Where to find documentation for `whois` command options?
What is the evidence that custom checks in Northern Ireland are going to result in violence?
Was Objective-C really a hindrance to Apple software development?
What were wait-states, and why was it only an issue for PCs?
When speaking, how do you change your mind mid-sentence?
What does the black goddess statue do and what is it?
When I export an AI 300x60 art board it saves with bigger dimensions
Co-worker works way more than he should
Why doesn't the university give past final exams' answers?
Protagonist's race is hidden - should I reveal it?
What happened to Viserion in Season 7?
All ASCII characters with a given bit count
What is a 'Key' in computer science?
Search throug server data does not work in Angular
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How does JavaScript .prototype work?How does the “this” keyword work?how does Array.prototype.slice.call() work?Why does JavaScript only work after opening developer tools in IE once?How does data binding work in AngularJS?How does Access-Control-Allow-Origin header work?How do I pass data to Angular routed components?Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'How to merge 2 searches in Angular Heroes tutorial?How is it working this Angular pipe used to perform a dynamic search of terms inserted into an input tag in my view?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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<City[]>
private searchTerms = new Subject<string>()
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<City[]>
if (!term.trim())
return of([])
return this.http
.get<City[]>(`$this.citiesUrl/?query=$term`, httpOptions)
.pipe(
tap(_ => this.log(`found cities matching "$term"`)),
catchError(this.handleError<City[]>("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
add a comment |
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<City[]>
private searchTerms = new Subject<string>()
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<City[]>
if (!term.trim())
return of([])
return this.http
.get<City[]>(`$this.citiesUrl/?query=$term`, httpOptions)
.pipe(
tap(_ => this.log(`found cities matching "$term"`)),
catchError(this.handleError<City[]>("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
add a comment |
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<City[]>
private searchTerms = new Subject<string>()
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<City[]>
if (!term.trim())
return of([])
return this.http
.get<City[]>(`$this.citiesUrl/?query=$term`, httpOptions)
.pipe(
tap(_ => this.log(`found cities matching "$term"`)),
catchError(this.handleError<City[]>("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
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<City[]>
private searchTerms = new Subject<string>()
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<City[]>
if (!term.trim())
return of([])
return this.http
.get<City[]>(`$this.citiesUrl/?query=$term`, httpOptions)
.pipe(
tap(_ => this.log(`found cities matching "$term"`)),
catchError(this.handleError<City[]>("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
javascript
asked Mar 22 at 14:58
Mark JamesMark James
227
227
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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()
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
add a comment |
You forgot subscribe with switchMap!
As per the angular docs:
An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the
subscribe()method of the instance, passing an observer object to receive the notifications.
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
add a comment |
Your implementation is alright. You just have to unwrap the cities$ Observable in your Template using an async pipe. It's recommended to use the async pipe wherever possible. Just makes the code less verbose.
Doing something like this would help:
<ul>
<li *ngFor="let city of cities$ | async">
city.name
</li>
</ul>
Here's a Working Sample StackBlitz for your ref.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302422%2fsearch-throug-server-data-does-not-work-in-angular%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
add a comment |
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()
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
add a comment |
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()
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()
edited Mar 22 at 15:23
answered Mar 22 at 15:12
Martin PaucotMartin Paucot
19111
19111
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
add a comment |
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
Wehen I added subsctbe() I am getteing the error: prntscr.com/n1ih7n
– Mark James
Mar 22 at 15:17
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
The code is edited, can you retry ?
– Martin Paucot
Mar 22 at 15:23
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
Yes, check the screenshot: prntscr.com/n1ilgl
– Mark James
Mar 22 at 15:25
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
I just make a new cities array which is not Observable and this works fine. Thanks!
– Mark James
Mar 22 at 15:31
add a comment |
You forgot subscribe with switchMap!
As per the angular docs:
An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the
subscribe()method of the instance, passing an observer object to receive the notifications.
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
add a comment |
You forgot subscribe with switchMap!
As per the angular docs:
An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the
subscribe()method of the instance, passing an observer object to receive the notifications.
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
add a comment |
You forgot subscribe with switchMap!
As per the angular docs:
An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the
subscribe()method of the instance, passing an observer object to receive the notifications.
You forgot subscribe with switchMap!
As per the angular docs:
An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the
subscribe()method of the instance, passing an observer object to receive the notifications.
edited Mar 22 at 15:39
Nino Filiu
3,29641631
3,29641631
answered Mar 22 at 15:03
user3804427user3804427
1146
1146
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
add a comment |
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am new to angular, can you give me more information about this answer? To be able to undrestand.
– Mark James
Mar 22 at 15:08
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
I am using switchMap((term: string) => this.weaterService.searchCities(term))
– Mark James
Mar 22 at 15:10
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
maybe is problem cus that is in ngOnInit? That is called only once when component is called right?
– Mark James
Mar 22 at 15:11
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
Hi, switchMap((term: string) => this.weaterService.searchCities(term)).subscribe( data => this.data = data) its for example. Good luck!
– user3804427
Mar 25 at 7:50
add a comment |
Your implementation is alright. You just have to unwrap the cities$ Observable in your Template using an async pipe. It's recommended to use the async pipe wherever possible. Just makes the code less verbose.
Doing something like this would help:
<ul>
<li *ngFor="let city of cities$ | async">
city.name
</li>
</ul>
Here's a Working Sample StackBlitz for your ref.
add a comment |
Your implementation is alright. You just have to unwrap the cities$ Observable in your Template using an async pipe. It's recommended to use the async pipe wherever possible. Just makes the code less verbose.
Doing something like this would help:
<ul>
<li *ngFor="let city of cities$ | async">
city.name
</li>
</ul>
Here's a Working Sample StackBlitz for your ref.
add a comment |
Your implementation is alright. You just have to unwrap the cities$ Observable in your Template using an async pipe. It's recommended to use the async pipe wherever possible. Just makes the code less verbose.
Doing something like this would help:
<ul>
<li *ngFor="let city of cities$ | async">
city.name
</li>
</ul>
Here's a Working Sample StackBlitz for your ref.
Your implementation is alright. You just have to unwrap the cities$ Observable in your Template using an async pipe. It's recommended to use the async pipe wherever possible. Just makes the code less verbose.
Doing something like this would help:
<ul>
<li *ngFor="let city of cities$ | async">
city.name
</li>
</ul>
Here's a Working Sample StackBlitz for your ref.
answered Mar 22 at 15:41
SiddAjmeraSiddAjmera
16.3k31240
16.3k31240
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302422%2fsearch-throug-server-data-does-not-work-in-angular%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown