简体   繁体   English

如何在不刷新页面/组件的情况下从 Angular 中的数据库中获取不断变化的数据

[英]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.

相关问题 如何在不刷新整个页面的情况下从视图中刷新数据? 有角度的 - How to refresh data from view without refreshing the whole page? angular 如何在不刷新整个页面的情况下更新组件 - Angular - How to Update a Component without refreshing full page - Angular Angular2:刷新子组件上的页面后无法从服务获取数据 - Angular2: Can't get data from service after refreshing page on child component 我们如何使用node.js从angular 7应用程序中的mysql数据库中获取数据并将其显示在angular组件html页面上 - How can we get data from mysql database in angular 7 application by using node.js and display it on angular component html page 如何在不刷新的情况下更新 angular 页面? - How to update angular page without refreshing? 在Angular 7中运行另一个Component的方法而不刷新当前页面? - In Angular 7 run method of another Component without refreshing current page? 在 angular 中无法在不刷新整个页面的情况下更新 Navbar 组件 - Unable to update Navbar component without refreshing the entire page in angular 如何在不刷新 Ionic 页面的情况下从服务中获取数据? - How to fetch data from service without refreshing the page in Ionic? 从API访问实时数据(访问Dynamo DB数据),而无需(异步)刷新Angular 6中的页面 - Access Real-time Data from an API (accessing Dynamo DB data) without refreshing the page (asynchronously) in Angular 6 角度组件每 X 秒刷新一次页面上的数据 - refreshing data on page every X seconds for angular component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM