【问题标题】:How to call/Get the out parameter from an API to Angular4如何从 API 调用/获取 out 参数到 Angular4
【发布时间】:2020-01-05 23:08:44
【问题描述】:

在这里,我正在使用 Angular 4 Web 应用程序上的 post 方法。输入数据并单击确认按钮时,值(数据)被保存到数据库中。它工作成功,但我的问题是同时post 方法完成后,ID(返回值)正在从 DB 传递到 API(对于 Angular )以获取另一个函数参数。在这里如何将该值带到我的角度?我希望该值作为下一个函数参数。实际上,如果我的问题有任何问题,请原谅我。 (我想要从 api 到 angular 的 TransactionId 值)

这是我的打字稿(TS 文件)

 onClick() {
this.header = new IHeaderstock(this.userid, this.created, this.CompanyID, 
this.modified, this.modifieduserid, this.confirm, this.shopid);
this.headeritems.push(this.header);

this._enqService.CatchHeaderDetail(this.headeritems)
        .subscribe(value => {
            if (typeof value !== 'undefined' && value != null) {
                value.forEach(header => {
                    this.headeritems.push(this.header)
                });
            }
        },
            error => {
                console.error(error);
                this.statusMessage = "Problem with the service.Please try again after sometime";
            });
additem{
   this.items = new IStockitems(this.shopid,value.ItemCode, value.ItemDescription, value.PackingtypeName, value.Stock, this.TransactionId );
 }   //here this.TransactionId is the returned value from the above post method.

这是我的服务文件

      CatchHeaderDetail(stock: any)
    : Observable<IHeaderstock[]> {
    let stockheaderdetail = stock;
    console.log(stockheaderdetail)
    debugger;
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this._http.post('http://localhost:3071/api/Stockcountheader/' + 'Stock', stockheaderdetail, options)
        .map((response: Response) => <IHeaderstock[]>response.json())
        .catch(this.handleError);

}

这是我的 WebAPI(注意我的 API 成功返回值)

My API is(it is working fine)


 public class StockcountheaderController : ApiController
    {
        private adminv2Entities enqentities = new adminv2Entities();
       [HttpPost]
        private  IActionResult Stock([FromBody] List<spGetNewStockCountHeader_Result> 
 jsonvalues)    
{
  ObjectParameter TransactionId = new ObjectParameter("TransactionId", typeof(Int32));
  foreach (spGetNewStockCountHeader_Result Datastock in jsonvalues)
  {

    spGetNewStockCountHeader_Result Stockobject = new spGetNewStockCountHeader_Result();
    Stockobject.UserID = Datastock.UserID;
    Stockobject.created = Datastock.created;
    Stockobject.CompanyID = Datastock.CompanyID;
    Stockobject.modified = Datastock.modified;
    Stockobject.modifieduserid = Datastock.modifieduserid;
    Stockobject.confirm = Datastock.confirm;
    Stockobject.ShopId = Datastock.ShopId;
    enqentities.spGetNewStockCountHeader(Datastock.UserID, Datastock.created, Datastock.CompanyID, Datastock.modified, Datastock.modifieduserid, Datastock.confirm,Datastock.ShopId, TransactionId);
  }
return Ok(new { data = TransactionId.Value});    
}

【问题讨论】:

    标签: angular typescript angular-services angular4-forms angular4-httpclient


    【解决方案1】:

    问题是在您的 API 方法中您返回 return "Ok(new { data = TransactionId.Value});" 但在服务方法或函数中您正在返回

    IHeaderstock ARRAY .map((response: Response) => <IHeaderstock[]>response.json()),您应该在服务中以角度返回整数类型,或者将事务值更改为 IHeaderstock 数组。

    【讨论】:

    • 感谢您的回答。我有疑问?如何将交易值更改为 IHEADERSTOCK ARRAY?
    【解决方案2】:

    在 API 代码中,您返回的内容类似于包含整数值的 json 对象。在您指定的服务中,响应将是IHeaderStock 类型的值数组。

    因此,您可以在任一侧进行一些更改来解决此问题。一方面,您可以像这样从服务中删除 IHeaderStock&lt;&gt; 类型

      CatchHeaderDetail(stock: any)
    : Observable<any> {
    let stockheaderdetail = stock;
    console.log(stockheaderdetail)
    debugger;
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this._http.post('http://localhost:3071/api/Stockcountheader/' + 'Stock', stockheaderdetail, options)
        .map((response: Response) => response.json())
        .catch(this.handleError);
    

    }

    【讨论】:

    • 但是像这样我的 ts.component 有一个像这样的错误....Build:Parameter 'header' 隐式具有 'any' 类型。
    • 无论哪一行发生错误,您可能还没有在此处指定header 变量的类型。只需将header 更改为(header: any),它就会消失。
    • 在这一行............ value.forEach(header => //这里发生...... ....this._enqService.CatchHeaderDetail(this.headeritems) .subscribe(value => { if (typeof value !== 'undefined' && value != null) { value.forEach(header => { this.headeritems.推(this.header)});}
    • 是的,把header改成(header: any)试试看。
    • 你在做value.foreach,但你的响应不是一个json对象吗? forEach 适用于数组。如果可能的话,你能把它发布到 stackblitz 上并分享链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多