首页 > 解决方案 > 如何获取已创建记录的 ID?

问题描述

我想获取hour_id已创建记录的 id ()。我需要为此做些什么?添加hour_id到表格?或者是其他东西?

this.form = new FormGroup({
  teams_id: new FormControl(null, [Validators.required]),
  team_name: new FormControl({ value: "", disabled: true }, Validators.required),
  well: new FormControl(null, [Validators.required]),
  layer: new FormControl(null),
  chock: new FormControl(null)
})

onSave() {
  this._service.create(this.form.value).subscribe(
    () => {
      this._toast.success("Saved.");
      // console.log(this.form.controls.hour_id) ?
    },
    error => {
      this._toast.error(error.error.message);
    }
  )
}

标签: angular

解决方案


您需要添加一个与您将从服务器返回的名称相同的新表单控件。假设您要返回 value hour_id,请将 hour_id 添加到表单中。

this.form = new FormGroup({
  hour_id: new FormControl(null),
  teams_id: new FormControl(null, [Validators.required]),
  team_name: new FormControl({ value: "", disabled: true }, Validators.required),
  well: new FormControl(null, [Validators.required]),
  layer: new FormControl(null),
  chock: new FormControl(null)
})

onSave() {
  this._service.create(this.form.value).subscribe(
    () => {
      this._toast.success("Saved.");
      console.log(this.form.controls.hour_id.value) ?
    },
    error => {
      this._toast.error(error.error.message);
    }
  )
}

当您使用返回的响应将值修补到表单时,hour_id将会更新。


推荐阅读