API数据库中的Angular FormGroup设置值

时间:2018-11-08 09:11:36

标签: angular

大家好,当我从数据库API以角度形式显示值时遇到问题, 我从身份登录名employeeNumber中获取数据,然后将身份信息中的数据自动创建到我的api Employee中,之后我想将该api预订到我的表单中,其中有employeeNumber,名称和功能。

这是我的形象,我得到了价值 enter image description here 这是我的代码组件。

  public ngOnInit(): void {

    //from uis
    this.getDataFromUIS = this.employeeService.get();
    // this.bookingvehicleForm.patchValue(this.employeeMemberService.getUserLogin(this.getDataFromUIS.employeeNumber));
    this.tampung3 = this.employeeMemberService.getUserLogin(this.getDataFromUIS.employeeNumber)
    .subscribe(s => {
      this.employeModelTransaction = s;
    });
    console.log(this.getDataFromUIS);
    console.log(this.tampung3);
    this.buildForm();
  }

  // Set variable form
  private buildForm(): void {
    let bookingDate: any;
    if (this.bookingvehicle.bookingDate != null) {
      bookingDate = this.dateconverter.DateToDTP(new Date(this.bookingvehicle.bookingDate));
    }
    else {
      bookingDate = this.dateconverter.DateToDTP(new Date());
    }
    let startPeriodeBooking: any = this.dateconverter.DateToDTP(new Date(this.bookingvehicle.startPeriodeBooking));
    let endPeriodeBooking: any = this.dateconverter.DateToDTP(new Date(this.bookingvehicle.endPeriodeBooking));
    let startHoursOfDeparture: any = this.dateconverter.TimeToTP(new Date(this.bookingvehicle.startHoursOfDeparture));
    let endHoursOfDeparture: any = this.dateconverter.TimeToTP(new Date(this.bookingvehicle.endHoursOfDeparture));

    this.bookingvehicleForm = this.fb.group({
      'id': [this.bookingvehicle.id],
      'employee': [this.tampung3.employeeNumber, Validators.compose([Validators.required, Validators.maxLength(55)])],
      'name': [this.tampung3.name, Validators.compose([Validators.required, Validators.maxLength(30)])],
      'function' : [this.tampung3.function],
      //'function': [this.bookingvehicleForm.controls['function'].value.employee.function],
      'bookingDate': [bookingDate, Validators.compose([Validators.required, Validators.maxLength(30)])],
      'startPeriodeBooking': [startPeriodeBooking, Validators.compose([Validators.required, Validators.maxLength(20)])],
      'endPeriodeBooking': [endPeriodeBooking, Validators.compose([Validators.required, Validators.maxLength(20)])],
      'destination': [this.bookingvehicle.destination, Validators.compose([Validators.required, Validators.maxLength(30)])],
      'passenger': [this.bookingvehicle.passenger],
      'reason': [this.bookingvehicle.reason, Validators.compose([Validators.required, Validators.maxLength(50)])],
      'driveMode': [this.bookingvehicle.driveMode, Validators.compose([Validators.required, Validators.maxLength(20)])],
      'startHoursOfDeparture': [startHoursOfDeparture],
      'endHoursOfDeparture': [endHoursOfDeparture],
    });
    console.log(this.bookingvehicleForm);
  };

为什么我从服务中获得的价值是继承?我的问题是我无法从服务中获取价值并将价值显示在表单中

请帮助:)

public getUserLogin(employeeNumber: string){ 
   console.log(this.employeeMemberURL + 
   this.config.ApiEndPoint.master.Employee + "getUserLogin"+ "/" + employeeNumber);
   return this.http.get(this.employeeMemberURL + this.config.ApiEndPoint.master.Employee + "getUserLogin"+ "/" + employeeNumber)
                   .map(res => Helper.bufferToJSON(res)); 
}

1 个答案:

答案 0 :(得分:0)

您正在请求数据,并通过此指令将subscribe()方法的返回赋值给this.tampung3

this.tampung3 = this.employeeMemberService.getUserLogin(this.getDataFromUIS.employeeNumber)
    .subscribe(s => {
      this.employeModelTransaction = s;
    });

但是我认为您只需要数据,因此您不应该管理subscribe()返回类型。您正在使用以下语句将远程获取的数据分配给this.employeModelTransaction

this.employeModelTransaction = s;

然后您再也不会阅读它。 因此,我认为您应该执行以下操作:

this.employeeMemberService.getUserLogin(this.getDataFromUIS.employeeNumber)
        .subscribe(s => {
          this.tampung3 = s;
        });