Edit/update 使用 angular 和 firebase 的反应形式

Edit/update reactive form using angular and firebase

我正在尝试 edit/update elementId 的另一个视图中的表单。

员工-list.comonent.ts(视图 2):

 onEdit(empId:string){
this._router.navigate(['/RegisterEmployee',empId]);
//this.service.formData=Object.assign({},emp);
 }

单击它会导航到具有所选元素 ID 的另一个视图,但我如何从所选元素的数据库中填充表单数据并更新 firebase 数据库中的已编辑数据

employee.component.ts(视图 1)-Reactive-Form

 ngOnInit() {
//this.resetForm();
this.form = this.formBuilder.group({
  fullName:new FormControl('',[Validators.required,Validators.pattern('[a-zA-Z][a-zA-Z ]+')]),
  designation: new FormControl('',[Validators.required,Validators.pattern('[a-zA-Z][a-zA-Z ]+')]),
  Email: new FormControl('',[Validators.required, Validators.email]),
  mobile: new FormControl('',[Validators.required, Validators.pattern('[0-9]*'),Validators.minLength(10),Validators.maxLength(10)])
});
 }

 /* submit the data to be added into database having 'employees' as the data 
 collection array*/
 onSubmit():void{
console.log(this.form.value);
this.router.navigateByUrl('/EmployeeList');
let data = Object.assign({},this.form.value);
if(this.form.value.id == null)
this.firestore.collection('employees').add(data);
else
this.firestore.doc('employees/'+this.form.value.id).update(data);
//this.resetForm(this.form);
this.toastr.success('Submitted successfully','Employee Register');
 }

我的服务如下: employee.service.ts

import { Injectable } from '@angular/core';
import { Employee } from './employee.model';
import { AngularFirestore } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
formData : Employee;

constructor(private firestore:AngularFirestore) { }

/*Function to get added employees list from firebase database*/
getEmployees(){
let listOfEmployees = 
 this.firestore.collection('employees').snapshotChanges();
 return listOfEmployees;
  }
   }

我的表单界面: employee.model.ts

export class Employee {
id : string;
fullName: string;
Email: string;
mobile: string;
designation :string;
}

请帮忙解决这个问题(angular 和我第一个使用 firebase 的项目)。

这里是工作应用程序 git 的存储库 https://github.com/HusnaKhanam/MyApplication

如果我没理解错的话,下面是用数据填充表单的基本步骤:

在员工组件中:

1) 从url参数中读取id类似这样:

  ngOnInit() {
    const param = this.route.snapshot.paramMap.get('id');
    if (param) {
      const id = +param;
      this.getProduct(id);
    }
  }

(注意:这是我的代码,您需要根据您的具体情况修改它。)

2) 从类似这样的服务中获取数据:

  getProduct(id: number): void {
    this.productService.getProduct(id)
      .subscribe(
        (product: Product) => this.displayProduct(product),
        (error: any) => this.errorMessage = <any>error
      );
  }

3) 使用类似于以下的代码更新表单上的数据:

  displayProduct(product: Product): void {
    this.product = product;

    // Update the data on the form
    this.productForm.patchValue({
      productName: this.product.productName,
      productCode: this.product.productCode,
      starRating: this.product.starRating,
      description: this.product.description
    });
  }