Angular router navigate doesn't load page when query parameters change

RoboDave

I have a component that display the result of an API query. I want to add a dropdown box so the list can be filtered but I can't seem to get the page to reload when the url query parameters change.

<div>
  <H2>Products</H2>
  <form>
    Filter By Manufacturer
    <select (change)="onManufacturerChange($event.target.value)">
      <option value="">All</option>
      <option [value]="m" *ngFor="let m of manufacturers">{{m}}</option>
    </select>
  </form>
  <ul>
    <li *ngFor="let p of products"><B>{{p.manufacturer}}</B> - {{p.name}}</li>
  </ul>
</div>

And the Typescript

export class ProductListComponent implements OnInit {

  public products: Array<object> = [];
  public manufacturers: any = [];
  public manufacturer: string = "";
  public search: string = "";

  constructor(private _assetService: AssetService, private router: Router, private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      console.log(params);
      if('manufacturer' in params){
        this.manufacturer = params.manufacturer;
      }
      if('search' in params){
        this.search = params.search;
      }
    })

    this._assetService.getManufacturers().subscribe(data => {
      this.manufacturers = data;
    })

    this.loadProducts(); 
  }

  loadProducts(){
      this._assetService.getProducts(this.manufacturer,this.search).subscribe((data: ProductListResult) => {
      this.products = data.products;
    }) 
  }

  onManufacturerChange(newValue){
    this.router.navigate(['/products'], {queryParams: {manufacturer: newValue}});
  }
}

When I change the item selected in the drop down box the URL displayed in the browser changes but the page does not reload. If I refresh the browser manually with the desired URL the correct output is shown. If I change the path in route.navigate to a totally different route that works correctly. If I add a call to loadProducts() after router navigate some it reloads the page but one selection out of phase.

Everything I've read says that if the array referenced in ngFor changes it should automagically update the DOM but I can't seem to trigger it. Do I need to call some sort of refresh or invalidate routine to update the DOM? Do I need to set something in the router to recognize changes to the query parameters? Am I going about this all wrong?

RoboDave

As far as I can tell there is no elegant way to get Angular to reload the page when the query parameters change.

The error in my code is that loadProducts() uses this.manufacturer so I need to set that in onManufacturerChange(). It doesn't reload the page but it does reload the data and update the DOM.

Then I need to create an updateRoute() function that is called after subscribe has loaded the data from the api. The Back and Forward buttons in the browser still don't work (it steps through the URLs but doesn't update the display) but the URL and the display otherwise stay in sync. Close enough for now.

  loadProducts() {
    this._assetService.getProducts(this.manufacturer,this.search).subscribe((data: ProductListResult) => {
      this.products = data.products;
      this.updateRoute();
    })
  }

  onManufacturerChange(newValue){
    this.manufacturer = newValue;
    this.loadProducts();
  }

  updateRoute( ){
    var queryParams = {};
    if (this.manufacturer!= "" ){queryParams['manufacturer'] = this.manufacturer;}
    if (this.search!= "" ){queryParams['manufacturer'] = this.search;}
    this.router.navigate(['/products'], {queryParams: queryParams});
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Router change the address but doesn't load the second page

React Router doesn't load the new page component after redirecting via navigate

Angular2 router - Navigate to the current page with different parameters

Angular 5 router doesn't match when query params added

page not load from top when navigate to new page in angular

Navigate in react router 6 doesn't redirect to another page

Angular 6 when redirected to home, page redirects but doesn't load

Angular 6 - Router can't navigate to next page

Angular router doesn't navigate for the first time from component to component

Router.navigate doesn't work in angular app

Why my router navigate function doesn't work in angular 8?

this.router.navigate() doesn't navigate to the URL

Angular 7 navigate is changing URL accordingly BUT doesn't load the component

React router doesn't load page from other component page

how to load progress when Angular change the page?

React router - page doesn't change when navigating from outside component

Curtainjs doesn't load when changing page

Angular 4, Router 4.0.0: routerLink or router.navigate programatically doesn't redirect to child routes

Data fetching not happening when I use two parameters in React Router Dom and navigate directly on the page

page doesn't load on refresh - react-router-dom

Nested react router component doesn't load/render on page reload?

angular-ui-router nested views the page doesn't loading when direct url is used or refresh

router.navigate with query params Angular 5

Angular Change Detection not triggering on Router.navigate()

Backbone router.navigate doesn't redirect

How to navigate to a specific section of the page on page load using React Router?

React JS React-router-dom6 doesn't navigate to the page by useNavigate

Router navigate doesn't save url after navigate

React Router page address change but doesn't refresh