Empty list is shown after adding a record

I have a page showing a list of items retrieved with an http service, and a button to add a new one. After adding a new one, I navigate programmatically to the first page, but now the service is not invoked and the list is empty.

BikeTypesComponent

@Component({
    selector: 'app-bike-types',
    imports: [
      MatListModule, 
      FormsModule, 
      ReactiveFormsModule,
      MatButtonModule,
      MatIconModule
    ],
    template: `
      <mat-selection-list #bikeTypesSelectionList [multiple]="false">
        @for (bikeType of bikeTypesList; track bikeType.id) {
          <mat-list-option [value]="bikeType.id">{{bikeType.name}}</mat-list-option>
        }
      </mat-selection-list>
    `
})
export class BikeTypesComponent {
    auth = inject(Auth);
    private router = inject(Router);
    bikeTypesList: BikeType[] = [];
    bikeTypeService: BikeTypeService = inject(BikeTypeService);

    loadBikeTypes(): void {
        this.bikeTypeService.getAll()
            .subscribe({
                next: (data) => {
                    this.bikeTypesList = data;
                },
                error: (err) => {}
            });
    }
    constructor() {
        this.loadBikeTypes();
    }
}

And, after saving the new BikeType, in the addBike‘s component code, I use this for navigating back to the bike-types page:

this.router.navigateByUrl('bike-types');

But the bike-types list is not loaded. The only way to have it loaded and displayed is to navigate to the page using the main menu, which ultimately is using this template code:

<a mat-list-item [routerLink]="bike-types">

0

Can you try below code:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatListModule } from '@angular/material/list';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { Router, NavigationEnd } from '@angular/router';
import { BikeTypeService } from './bike-type.service';
import { BikeType } from './bike-type.model';
import { Auth } from './auth.service';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-bike-types',
  imports: [
    MatListModule, 
    FormsModule, 
    ReactiveFormsModule,
    MatButtonModule,
    MatIconModule
  ],
  template: `
    <form [formGroup]="form">
      <mat-selection-list #bikeTypesSelectionList [formControl]="bikeTypesControl" name="bikeTypesList" [multiple]="false">
        <mat-list-option *ngFor="let bikeType of bikeTypesList" [value]="bikeType.id">{{bikeType.name}}</mat-list-option>
      </mat-selection-list>
    </form> 
  `
})
export class BikeTypesComponent implements OnInit {
  auth = inject(Auth);
  private router = inject(Router);
  bikeTypesList: BikeType[] = [];
  bikeTypeService: BikeTypeService = inject(BikeTypeService);
  form: FormGroup;
  bikeTypesControl = new FormControl();

  constructor() {
    this.form = new FormGroup({});
  }

  ngOnInit() {
    this.loadBikeTypes();

    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd)
    ).subscribe((event: NavigationEnd) => {
      if (event.urlAfterRedirects === '/bike-types') {
        this.loadBikeTypes();
      }
    });

    this.form = new FormGroup({
      bikeTypesList: this.bikeTypesControl
    });
  }

  loadBikeTypes() {
    this.bikeTypeService.getAll()
      .subscribe({
        next: (data) => {
          this.bikeTypesList = data;
        },
        error: (err) => {}
      });
  }

  navigateToRoute(route: string) {
    this.router.navigate([route]);
  }
}

When a NavigationEnd event occurs, check if the URL is /bike-types. After checking, call the loadBikeTypes method to reload the data. I have moved the data loading logic into a separate loadBikeTypes method so that it can be called both in ngOnInit and when the route is re-entered without duplicating the code. This ensures that the list is reloaded when you navigate back to the /bike-types route regardless of programmatically or through the main menu.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật