首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与在Angular 7中创建和编辑数据的表单相同

与在Angular 7中创建和编辑数据的表单相同
EN

Stack Overflow用户
提问于 2019-02-09 23:47:17
回答 1查看 1.7K关注 0票数 0

我已经创建了一个反应式表单来创建数据并将其插入到数据库中,但我在编辑行时遇到了问题。首先,我必须在路由模块中创建一条路由,并给出它的方向(如path: 'patient/:id', component: PatientComponent。这里的/patient是我的主页)。

其次,当我单击编辑按钮时,它会加载包含相关数据的表单,但当我单击提交按钮时,特定行不会进行编辑,而是会将具有新$key的新记录添加到表中。

我想单击编辑按钮和不加载的表单(省略/patient/{{patient.$key}}),当我单击提交按钮时,它应该会修改数据库中的数据。

谢谢..。

下面是我的组件代码:

代码语言:javascript
复制
patientIdUpdate = null; 

ngOnInit() {  
    this.updatePatientData();
    const id = this.actRoute.snapshot.paramMap.get('id'); 
    this.crudApi.GetPatient(id).valueChanges().subscribe(data => {
    this.patientForm.setValue(data)
    })

    this.crudApi.GetPatientsList();
    this.patientsForm(); 
    this.dataState();
    let s = this.crudApi.GetPatientsList(); 
    s.snapshotChanges().subscribe(data => {
      this.Patient = [];
      data.forEach(item => {
        let a = item.payload.toJSON(); 
        a['$key'] = item.key;
        this.Patient.push(a as Patient);
      })
    })
  }

dataState() {     
    this.crudApi.GetPatientsList().valueChanges().subscribe(data => {
      this.preLoader = false;
      if(data.length <= 0){
        this.hideWhenNoPatient = false;
        this.noData = true;
      } else {
        this.hideWhenNoPatient = true;
        this.noData = false;
      }
    })
  }

submitPatientData(patient: Patient) {
    if(this.patientIdUpdate == null) {
    this.crudApi.AddPatient(this.patientForm.value);
    this.router.navigate(['patient']);
    this.ResetForm();
    } else {
    patient.$key = this.patientIdUpdate
    this.crudApi.UpdatePatient(this.patientForm.value);  
    this.router.navigate(['patient']);
    this.ResetForm();    
    }

   };

下面是我的标记代码:

代码语言:javascript
复制
<form [formGroup]="patientForm" (ngSubmit)="submitPatientData()">

<tr *ngFor="let patient of Patient | paginate: { itemsPerPage: 7, currentPage: p }; let i = index;">
              <td class="text-center action-block">
                  <button type="button" class="btn btn-info"><i class="fa fa-pencil-square-o" routerLink="/patient/{{patient.$key}}"></i></button>
                  <button type="button" class="btn btn-danger"><i class="fa fa-trash-o" (click)="deletePatient(patient)"></i></button></td>
              <td>{{patient.FirstName}} {{patient.LastName}}</td>
              <td>{{patient.FatherName}}</td>
              <td>{{patient.Address}}</td>
              <td>{{patient.NC}}</td>
              <td>{{patient.Mobile}}</td>
              <td>{{patient.Date}}</td>
              <td>{{patient.Reason | number}}</td> 
EN

回答 1

Stack Overflow用户

发布于 2019-11-27 16:31:02

我的第一个猜测是检查您调用的服务

代码语言:javascript
复制
this.crudApi.UpdatePatient(this.patientForm.value);

它是否在使用PUT方法?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54607860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档