1

Want to send parameter in url in angular 7, so that the url should look like return example.com/search/users?q=tom.

I am using the following syntax in my service .

 public searchUsers(obj):any{
        return this._http.get('example.com/search/users?q=', obj)
    }

I want the url to be look like example.com/search/users?q=

2
  • Hello yash. I have read your question and I found your term 'send parameter in URL' very disturbing. Can you give more explanations. What do you want ? Loading a page with parameter for a specific URL ? Asking some information from another site to retrieve user information ? Why 'TOM' is in your text and why TOM is not more in your code ? Can you try to give us more information ? Thanks Commented Dec 5, 2018 at 14:18
  • he is trying to make an API call with query params in url Commented Dec 5, 2018 at 16:43

2 Answers 2

3

You should be able to do it using HttpParam.

const params = new HttpParams()
        .set('q', 'value here')
        .set('another_param', 'value_here');
return this.httpClient.get('example.com/search/users', { params })
Sign up to request clarification or add additional context in comments.

Comments

0

Addon to the correct answer:

If you are wondering why no parameters get passed, there is a key hint in the referenced HttpParams docs

This class is immutable - all mutation operations return a new instance.

That means this will work:

let params = new HttpParams();
if ( limit ) {
  params.set('limit', String(limit));
}
params.set('search', keyword);

If you want to append HttpParams conditionally, use a variation of this instead:

let params = new HttpParams();
if ( limit ) {
  params = params.set('limit', String(limit));
}
params = params.set('search', keyword);

Learned this the hard way. Hope this helps any other TS beginners to save a bit of confusion...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.