skip to content
Alvin Lucillo

Set search params

/ 1 min read

💻 Tech

Instead of handling URL parts manually, we can use URL and HttpParams. HttpParams is from the Angular library, and URL is part of the standard library. Using these class will help create safe URLs and provide ease of use for composing the values. In the example below, we set the URL and its search params.

    const params = new HttpParams().set('id', '1').set('order', 'sc');
    const url = new URL("https://test.com");
    url.search = params.toString();
    console.log(url.toString()); // https://test.com/?id=1&order=asc