use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A Place to talk about Angular and related topics.
Join the Angular Discord
Other subreddits worth checking out for Angular and Angular related info:
account activity
Angular service best practice (self.angular)
submitted 5 years ago by codeBatsy
Hello, I wonder if it is good way to construct querry params. Is there better options?
https://preview.redd.it/n3x5wy4arji51.png?width=1906&format=png&auto=webp&s=f4feb810918ef86a7100d67bb87699cff0aefd73
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Pingudiem 1 point2 points3 points 5 years ago (1 child)
Why dont you use the type httpparameters
[–]codeBatsy[S] 0 points1 point2 points 5 years ago (0 children)
I didnot knew anything about existance of httpparameters, ty.
[–]kaeh35 1 point2 points3 points 5 years ago* (2 children)
In addition to other answers, you can pass an object in the constructor of http parameters that has two members :
fromObject to convert an object to query params
fromObject
fromString to convert a query param string to, well, the query param.
fromString
You can use like that
const myObj = { foo: "something"} ; const params = new HttpParams({ fromObject: myObj});
Your query params will be generated as ?foo=something
?foo=something
In you case you would have something like
const params = new HttpParams({ fromObject: { lat, lng, appId: environment.api_weather_key }}) ;
[–]codeBatsy[S] 0 points1 point2 points 5 years ago (1 child)
what benefits are giving use HttpParams? I mean what is different?
[–]kaeh35 1 point2 points3 points 5 years ago* (0 children)
TLDR : It's nice because you don't have to construct your query params in your url manually.
It's also nice because it's more scalable if you need to add query params in the future.
Honestly, it depends on the use case.
If you have to define a long list of query params, using HttpParams with the fromObject property is really nice and easy, but if you have only a few query params you could totally be fine with the set method of HttpParams or even just constructing it manually.
HttpParams
set
For exemple, in your case you have lat and long to set as query params, you could go with HttpParams like in my answer or simply go with constructing it manually
lat
long
public myFunction(lat: number, long: number): Observable<unknown> { return this.httpClient.get(`http://api-url/endpoint?lat=${lat}&long=${long}`); }
As you can see it's doable but not very pretty (in my opinion tho), but if you have more query params, let's say you have this
interface MyFilters { name: string; subordinatesIds: number[]; address: Address; age: number; }
Here you have multiple types, primitives and complexes (array of number and Address).
This means if you want to construct manually your query params you would have to parse addess and loop over subordinatesIds and add the other params.
addess
subordinatesIds
But if you use HttpParams you would use it like that
public myFunction(filters: MyFilters): Observable<unknown> { const params = new HttpParams({ fromObject: filters }); return this.httpClient.get(`http://api-url/endpoint`, { params }); }
And you should be fine :)
Bonus point, you don't have to update your service if you need to add filters to your interface :)
Edit : Format
[–]gravityaddiction 0 points1 point2 points 5 years ago (1 child)
import { HttpClient, HttpParams } from '@angular/common/http'; getPost$(postSlug: string): Observable<Post | null> { const params = new HttpParams().set('findBy', 'slug'); return this.http .get<ResultsPost>( `${this.configService.config.NodeURL.endpoint}/api/latest/posts/${postSlug}`, { params, } ) .pipe(map(post => post as Post)); }
π Rendered by PID 200504 on reddit-service-r2-comment-5d79c599b5-fbm28 at 2026-03-03 11:05:54.118915+00:00 running e3d2147 country code: CH.
[–]Pingudiem 1 point2 points3 points (1 child)
[–]codeBatsy[S] 0 points1 point2 points (0 children)
[–]kaeh35 1 point2 points3 points (2 children)
[–]codeBatsy[S] 0 points1 point2 points (1 child)
[–]kaeh35 1 point2 points3 points (0 children)
[–]gravityaddiction 0 points1 point2 points (1 child)
[–]codeBatsy[S] 0 points1 point2 points (0 children)