1

I am migrating Angular JS to Angular 7. I am looking at this code and trying to implement in Angular 7.

In the service, $location was injected, and the following methods reset and set the query parameters.

function resetAllOptions() {
            // Clears query params
            $location.search('');

}

function setQueryParameters() {
    // Sets query parameters
            $location.search({
                searchType: searchType,
                searchField: searchField,
                searchValue: searchValue,
                searchValueTwo: searchValueTwo,
                searchValueThree: searchValueThree
            });
}

How do I implement this in Angular 7?

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
Anu
  • 11
  • 1
  • 3

2 Answers2

1

Parameters are done completely differently in Angular v7 as they are a part of Routing. So there is no direct line to line equivalent of what you are trying to accomplish.

In Angular v2+, there are three different types of parameters, so your first step is to define the type that you want.

Here is a post that describes the different types in detail:

Send data through routing paths in Angular

Assuming you want to stick with Query parameters:

You can set them in the HTML like this:

          <a [routerLink]="[product.id]"
             [queryParams]="{filterBy: listFilter, showImage: showImage}">
            {{ product.productName }}
          </a>

Or in code like this:

this.router.navigate([`/search`],
              {queryParams: {
                     searchType: searchType,
                     searchField: searchField, // ...
               }});
DeborahK
  • 57,520
  • 12
  • 104
  • 129
0

Here is the source code to $location.search():

https://github.com/angular/angular.js/blob/47bf11ee94664367a26ed8c91b9b586d3dd420f5/src/ng/location.js#L560-L591

Matthias
  • 13,607
  • 9
  • 44
  • 60