【问题标题】:Click reset button followed by click submit button goes to UPDATE call instead of POST call单击重置按钮,然后单击提交按钮转到 UPDATE 调用而不是 POST 调用
【发布时间】:2020-09-05 20:47:28
【问题描述】:

第一次提交提交按钮后调用成功,第二次进入更新调用。如果我在提交按钮调用后单击重置按钮也会去更新 cal.l 任何人都可以帮助我吗?

组件代码

ngOnInit() {
    this.resetForm();
    this.refreshEmployeeList();
}

resetForm(form?: NgForm) {
    if (form)
      form.reset();
    this.employeeService.selectedEmployee = {
      _id: "",
      name: "",
      position: "",
      office: "",
      salary: null
    }
}

onSubmit(form: NgForm) {
    if (form.value._id == "") {
      this.employeeService.postEmployee(form.value).subscribe((res) => {
        this.resetForm(form);
        this.refreshEmployeeList();
      });
    }
    else {
      this.employeeService.putEmployee(form.value).subscribe((res) => {
        this.resetForm(form);
        this.refreshEmployeeList();
      });
    }
}

【问题讨论】:

    标签: angular


    【解决方案1】:

    使用if (!form.value._id) { 代替if (form.value._id == "") {
    这样您就不会检查_id 是否为空字符串,而是检查其值是否为falsy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-02
      • 2017-08-14
      • 1970-01-01
      • 2016-05-31
      • 2021-09-29
      • 2012-03-29
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多