0

How to pass url arguments (query string) to a HTTP request as

let params: URLSearchParams = new URLSearchParams();

params.set('pageId', 0);
params.set('activeFilter', 1);

let requestOptions = new RequestOptions();
requestOptions.search = params;

return this._http.get("http://testsite.com/cats/page/", requestOptions ).map(res => res.json());

where in my requestOptions reference i want to pass like :pageId/:activeFilter .

finally i need to build an url to call my rest api server using this url ( http://testsite.com/cats/page/:pageId/:activeFilter ) //pageId=0,activeFilter=1

i have to bind only the value not the whole string?

i tried a lot. but it is not coming as i expected, this is what i am getting /cats/page/?pageId=0&activeFilter=1 in my server console.

any suggestions please? unable to find where i am going wrong

3
  • /cats/page/?pageId=0&activeFilter=1 seems correct. What else should it be? Commented May 19, 2017 at 10:24
  • 1
    You mean http://testsite.com/cats/page/0/1 ? If so then just construct the URL. With string template if necessary. Commented May 19, 2017 at 10:26
  • the last two values will change page to page dynamically. in my server side routes i am using the same url to get all categories from server. if suppose when i click load more button pageId value will change to 1 and so on.. Commented May 19, 2017 at 10:31

2 Answers 2

2

ECMA6 has provided one very interesting feature "String Interpolation" where string can have placeholders which we replaced as per the requirement.

Ex:

let first = 'Jane';
let last = 'Doe';
console.log(`Hello ${first} ${last}!`);

In your case, It would be:

let param1 = 'someval1';
let param2 = 'someval2'
this._http.get(`http://testsite.com/cats/page/${param1}/${param2}`, requestOptions ).map(res => res.json());
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to pass those values into the url you should put them directly into the url string like this:

@Injectable()
export class ExampleService {

  constructor(
    private http: Http
  ) { }

  getPage(pageId: number, activeFilter: number) {
    return this.http.get(`http://testsite.com/cats/page/${pageId}/${activeFilter}`)
                    .map(res => res.json())
  }

}

Note that you're using `` to allow for string interpolation.

You would call this service method dynamically from a component by passing in the pageId and activeFilter like so:

import { Component, OnInit } from '@angular/core'; import { ExampleService } from './example.service';

@Component({
  selector: 'example',
})

export class ExampleComponent implements OnInit {
  constructor(
    private exampleService: ExampleService
  ) { }
  currentPageId = 0;
  activeFilter = 1;

  ngOnInit() { }

  getPage() {
    this.exampleService.getPage(this.currentPageId, this.activeFilter)
  }
}

Hope that clarifies things. Good luck!

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.