【问题标题】:HTTP Service call bind to HTML elementsHTTP 服务调用绑定到 HTML 元素
【发布时间】:2019-05-26 09:40:36
【问题描述】:

我有一个 HTML 页面,我在其中显示患者列表。 (从服务调用调用到 json 服务器的列表)。当用户点击一个病人时,它应该显示页面的详细信息。

在服务中

getPatientList(): Observable<any> {
  return this.http.get<any[]>(endpoint).pipe(
  tap(data => console.log( JSON.stringify(data)))
 );
}

getPatientDetails(id: number): Observable<any> {
      return this.getPatientList().pipe(
      map((patient: any[]) => patient.find(p => p.profile_no === id))
      );
}

患者详细信息.ts

import { Component, OnInit, Input } from '@angular/core';
import { DataService } from '../services/data.service';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-patient-details',
  templateUrl: './patient-details.component.html',
  styleUrls: ['./patient-details.component.css']
})
export class PatientDetailsComponent implements OnInit {

  patient : any;
  pId: number;

  constructor(private data: DataService, private route: ActivatedRoute) { }
  ngOnInit() {


    this.data.getPatientDetails(this.pId).subscribe(
      patient => this.patient = patient);
    console.log(this.patient);
  }
}
console.log(patient) display following in console.


Observable {_isScalar: false, source: Observable, operator: MapOperator}

这是HTML模板的一部分

<div class="form-group child">
      <h1>{{patient.first_name}} {{patient.last_name}}</h1>
    <div class="col form-group form-control-md mt-5">
      <label for="profileNo" style="width:25%">Profile No: </label>
      <label></label>
      <input type="text" class="input-group-sm" [(ngModel)]="patient.profile_no"> 
    </div>  
    <div class="col form-group form-control-md">
      <label for="DOB" style="width:25%">Date of Birth: </label> 
      <input type="text" class="input-group-sm" [(ngModel)]="patient.DOB">
    </div> 
    <div class="col form-group">
      <label for="gender" style="width:25%">Gender: </label>
      <input type="text" class="input-group-sm"[(ngModel)]="patient.gender"> 
    </div>
    <div class="col form-group">
      <label for="bloodGroup" style="width:25%">Blood Group: </label>
      <input type="text" class="input-group-sm" [(ngModel)]="patient.blood_group"> 
    </div>

【问题讨论】:

  • 解释你的问题
  • 请在您绑定的地方发布 HTML 模板,并发布 .TS 类。
  • 用html模板和ts编辑
  • 请问您的Id 是如何注入endpoint 的?意味着如果您尝试使用本地消费者(例如邮递员)测试您的服务,如何通过 id ? : endpoint/id , endpoint?id=id ...
  • 我已经运行了 json 服务器。在 json 服务器上,当我运行以下命令时,它为我提供了患者的完整详细信息。 localhost:3000/patient-details?profile_no=1

标签: angular


【解决方案1】:

我认为前两种方法(服务)应该重构为:

getPatientList(): Observable<any> {
  return this.http.get<any[]>(`${endpoint}`);
}

还有:

getPatientDetails(id: number): Observable<any> {
  return this.http.get<any[]>(`${endpoint}?profile_no=${id}`);
}

【讨论】:

  • @khushi,顺便说一句,如果您订阅了该服务,通常不应给出相同的错误。您在课堂服务中使用HttpHttpClient 吗?
  • 是httpclient
  • @khushi 根据你提供的内容,应该可以工作,即使你的console.log(patient) 不应该给Observable {_isScalar: false, source: Observable, operator: MapOperator},验证,重新运行你的服务器也许。
  • 我尝试重新运行,同样的错误。如果您可以给我其他解决方案,请告知。
  • 它返回相同的可观察对象 Observable {_isScalar: false, source: Observable, operator: MapOperator}
【解决方案2】:

欢迎来到堆栈溢出!

我看到的代码中的一个错误是您试图在检索值之前显示一个值

这段代码:

  ngOnInit() {
    this.data.getPatientDetails(this.pId).subscribe(
      patient => this.patient = patient);
    console.log(this.patient);
  }

应该是这样的:

  ngOnInit() {
    this.data.getPatientDetails(this.pId).subscribe(
      patient => {
         this.patient = patient;
         console.log(this.patient);
      });
  }

日志记录需要订阅中。

订阅告诉你的服务发送一个 Http 请求。提交该请求后,立即显示的值将始终未定义。

在稍后的某个时间点,返回数据并执行订阅中的代码。所以此时,您的patient 数据已设置好,您可以记录它了。

除此之外,我不确定您遇到了什么问题。

如果您可以将链接发布到您的 stackblitz 编辑会话(而不是运行代码),我们可以查看并提供进一步的帮助。

【讨论】:

  • 这里的重点是,他/她说错误总是Observable {_isScalar: false, source: Observable, operator: MapOperator} 这没有意义(至少对我来说),您要解决的问题应该是undefined ,不是吗?
  • 这看起来不像是“错误”?但你是对的,如果这是控制台中显示的内容,那么它一定来自其他而不是显示的代码。
  • DeborahK 的解决方案部分起作用。但现在代码返回空数组。
  • 这是我的 json 服务器返回的查询 localhost:3000/patient-details?profile_no=1[ { "profile_no": 1, "first_name": "Jeanette", "last_name": "Penddreth", "DOB": "4 /24/1990”,“性别”:“女性”,“insurance_no”:“INS7346”,“blood_group”:“B +”}]
猜你喜欢
  • 2018-05-29
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多