[英]How to get the constantly changing data from database in Angular without refreshing the page/component
I'm working on an Angular project where my database in mongoDB and backend in flask.我正在开发一个 Angular 项目,其中我的数据库位于 mongoDB 中,后端位于 flask 中。 My data inside one collection keeps on changing after every 10 seconds.我在一个集合中的数据每 10 秒就会不断变化。 I'm writing REST APIs to share data between Angular and MongoDB.我正在编写 REST API 以在 Angular 和 MongoDB 之间共享数据。 What should I do to get the latest data in my database without refreshing the page every time.我应该怎么做才能在我的数据库中获取最新数据而无需每次都刷新页面。
This is my div whose heart beat,BP,O2,PR changes every 10secs.这是我的 div,其心跳、BP、O2、PR 每 10 秒变化一次。
<ng-template #not_distressed>
<div class="card-header" style="color: rgb(0, 0, 0); ">
<b>Ward No : {{pateint.ward_no}}</b>
</div>
<div class="card-body" style="color: rgb(0, 0, 0); ">
<div style="text-align: left;"></div>
<div style="text-align: right;"></div>
<h5 class="card-title"><b>Name : </b>{{pateint.patient_name}}</h5>
<p class="card-text">
<b>Heart beat : </b>{{pateint.heart_rate}}<br>
<b>B.P. : </b>{{pateint.BP}}<br>
<b>O2 : </b>{{pateint.O2}}<br>
<b>P.R. : </b>{{pateint.RR}}
</p>
</div>
</ng-template>
typescript file typescript 文件
import { Component, OnInit } from '@angular/core';
import { PatientVitalsService } from '../services/patient-vitals.service';
@Component({
selector: 'app-patient-vitals',
templateUrl: './patient-vitals.component.html',
styleUrls: ['./patient-vitals.component.css']
})
export class PatientVitalsComponent implements OnInit {
public patientVital={};
constructor(private _patientListService: PatientVitalsService) { }
ngOnInit() {
this.dataService.getdetails().subscribe((data: any)=>{
console.log("In service",data);
this.repos = data;
this.patientVital = this.repos['result']
})
}
}
Service.ts file服务.ts 文件
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable, Subject, throwError, BehaviorSubject } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ajax } from 'rxjs/ajax';
@Injectable({
providedIn: 'root'
})
export class DataService {
get_symptoms = "http://127.0.0.1:5000/api/getdetails"
getdetails(): Observable<any> {
return this.httpClient.get(this.get_details)
}
}
You can implement a polling mechanism which will request data every 10 seconds.您可以实施轮询机制,每 10 秒请求一次数据。 You just need to extend you existing function and tie it to timer
from RxJs.您只需要扩展现有的 function 并将其与 RxJs 的timer
相关联。 Then instead of returning number use switchMap
to return you desired value.然后,不要返回数字,而是使用switchMap
来返回您想要的值。 Other operators are to make it more robust其他运营商将使其更加健壮
import { Subject, timer} from 'rxjs';
import { switchMap, share, retry, takeUntil } from
export class DataService {
get_symptoms = "http://127.0.0.1:5000/api/getdetails"
private stopPoll$: Subject<boolean> = new Subject<boolean>();
getdetails(): Observable<any> {
this.stopPoll$.next(false); // Reset stopPoll$ when needed again
// start at 1st millisecond, repeat every 10
return timer(1, 10000).pipe(
switchMap(() => this.httpClient.get(this.get_details)), // GET data
retry(), // retry if failure
share(), // share -> do not create new subscribtion on a new subscribe
takeUnitl(this.stopPoll$) // to have the ability to stop the poll
)
}
stopPolling(): void {
this.stopPoll$.next(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.