在Angular 6 TS

时间:2019-02-15 06:48:14

标签: angular typescript web-services asp.net-web-api angular6

我无法在我的TS中呼叫邮政服务。我正在以某种形式从用户那里获取数据,并将此数据保存到数组中并在表中显示该数据。之后,我单击另一个按钮来保存我的数据,该按钮将调用不起作用且未显示任何错误的服务。我还在此按钮上调用了另外两个服务,单击它可以正常工作,但我的阵列服务不起作用。

Service.ts

insertAddresses(formData: SettingsAddressesModel) {
    return this.http.post(this.apiURL + "/SettingAddress/SaveAddress", formData);
  }

Component.ts

insertAddressList(compIDforAddress){
    for(var i = 0; i < this.childSettingAddressComponent.SettingsAddressesList.length; i++){
      this.childSettingAddressComponent.SettingsAddressesList[i].ElementID = compIDforAddress;
      //this.addressService.insertAddresses(this.childSettingAddressComponent.SettingsAddressesList[i]);
      console.log('address data', this.childSettingAddressComponent.SettingsAddressesList[i]);
    }
    this.addressService.insertAddressListArray(this.childSettingAddressComponent.SettingsAddressesList);
    console.log('Address Elment ID:', compIDforAddress);
  }

2 个答案:

答案 0 :(得分:1)

您需要预订任何异步调用,否则它们将无法执行。

component.ts中修改您的代码,如下所示:

  this.addressService.insertAddressListArray(this.childSettingAddressComponent.SettingsAddressesList)
.subscribe((result)=>{

    // result will contain the response from service  
     console.log('Address Elment ID:', compIDforAddress); 
    });

答案 1 :(得分:1)

订阅并记录错误(如果有的话),因为如果处理不当,post方法在大多数情况下会静默失败。

this.addressService.insertAddressListArray(this.childSettingAddressComponent.SettingsAddressesList)
.subscribe((result)=>{    
    // result will contain the response from service  
     console.log('Address Elment ID:', compIDforAddress); 
    },
     (error)=>{
     console.log("error "+error);
     }
);