简体   繁体   English

如何在angular2中调用具有相同功能的2个api

[英]How to call 2 api with same function in angular2

i have save function, so if the data is empty then post method must be called,if there is data already then put method must be implemented. 我有保存功能,所以如果数据为空,则必须调用post方法,如果已经有数据,则必须实现put方法。 Can anyone help me to solve that. 谁能帮我解决这个问题。

Ts: Ts:

    saveEmergencyContact(itemrow) {
    console.log(itemrow,"itemrow");
    if(itemrow.Id!=null) {
      this._userService.editEmergencyContact(itemrow.Id).subscribe(res => {
        console.log(res,"edit");
        this.showSuccess("Emergency Contact updated succesfully");
        this.getEmergencyContact();
      })
    } else {
    this._userService.saveEmergencyContact(itemrow).subscribe(res => {
      console.log(res,"add");
      this.showSuccess("Emergency Contact saved succesfully");
      this.getEmergencyContact();
    })
  }
}

If there is no Id, then it must work as above function, and if incase it has Id it must work as put function. 如果没有Id,则它必须像上面的函数一样工作,如果它有Id,则它必须像put函数一样工作。 Please help me out. 请帮帮我。

Here if i edit on the already data having section, it generates new section and i see 2 sections extra below that. 在这里,如果我在已经具有数据的部分上进行编辑,它将生成新的部分,而我在其下看到2个部分。 once after refresh, there will be one extra field. 刷新后,将有一个额外的字段。

Update: 更新:

 create() {
    return this._fb.group({
      ContactName: '',
      Phone: '',
      Relation: '',
    });
  }

I guess that's what you're looking for: 我想这就是您要寻找的东西:

 saveEmergencyContact(itemrow) {
    if(itemrow.id!=null)
    {
      this._userService.editEmergencyContact(itemrow).subscribe(...)
    }
    else
    {
      this._userService.addEmergencyContact(itemrow).subscribe(...)
    }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM