简体   繁体   中英

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. Can anyone help me to solve that.

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. 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. 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(...)
    }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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