пациенто-add.component.ts
mponent
<app-navbar></app-navbar>
<div class="row">
<div class="col-lg-4">
<app-patients></app-patients>
</div>
<div class="col-lg-8">
<router-outlet></router-outlet>
</div>
</div>
оказание услуг
private readonly URL = 'http://localhost:8080/api/patients';
constructor(private httpClient: HttpClient) {
}
public getPatient(id: number): Observable<Patient> {
const url = `${this.URL}/${id}`;
return this.httpClient.get<Patient>(url);
}
public getPatients(): Observable<Patient[]> {
console.log('inside service getPatients');
return this.httpClient.get<Patient[]>(this.URL);
}
public addPatient(patient: Patient): Observable<Patient> {
return this.httpClient.post<Patient>(this.URL, patient);
}
Метод addPatient внутри PatientAddComponent
constructor(private patientService: PatientService, private router: Router, private patientsComp: PatientsComponent) {
}
ngOnInit() {
}
addPatient() {
this.patientService.addPatient(this.patient).subscribe((patient) => {
//redirection to page with new patient. This part works fine.
this.router.navigateByUrl(patient.id.toString());
}
);
Компонент с массивом
patients: Patient[];
constructor(private patientService: PatientService) {
}
ngOnInit() {
console.log('inside patients on init');
this.patientService.getPatients().subscribe(patients => {
this.patients = patients;
});
}
console.log показывает, что массив действительно обновлен, поэтому похоже, что до этой части все работает нормально, но изменения не отражаются в HTML-коде компонента Patients:
<div id="overflowControl">
<div *ngFor="let patient of patients" class="list-group" id="list-tab" role="tablist" [routerLink]="['/', patient.id]">
<div class="container">
<a class="list-group-item list-group-item-action" id="list-home-list" data-toggle="list" href="#list-home"
role="tab" aria-controls="home" (click)="toggleActive()">
<div class="row">
<div class="col-lg-9">
<p class="pt-2 pl-2">{{patient.name}}</p>
<p class="pl-2">{{patient.date | date: 'short'}}</p>
</div>
<div class="col-lg-3">
<h3 class="pt-2"><i class="fa fa-female sex-icon" *ngIf="patient.sex == 'female'"></i></h3>
<h3 class="pt-2"><i class="fa fa-male sex-icon" *ngIf="patient.sex == 'male'"></i></h3>
</div>
</div>
</a>
</div>
</div>
</div>
Я должен вручную обновить страницу браузера, и только тогда я увижу список объектов с недавно добавленным. Как мне убедиться, что список объектов обновляется, когда в него добавляется новый объект? Вот текущая версия Angular, с которой я работаю:
ссылка на проект github:https://github.com/kolos181/med-records/tree/master/src/app/components