【问题标题】:How to fetch a field from the response which we are getting from the API in Angular 6如何从我们从 Angular 6 中的 API 获得的响应中获取字段
【发布时间】:2018-09-07 16:52:55
【问题描述】:

我的问题是我没有任何 getAll() Web API 方法。当我点击我的 Angular 应用程序时,我唯一能看到的是网络,整个 JSON 都可以通过网络http:localhost:8080/api 获得。现在我只想要一个字段,即来自获得的 json 的contractId

{
  "contractId": 44,
  "contractName": "BOND",
  "contractServiceList": [
    {
      "id": 44,
      "serviceId": 1,
      "providerTier": null,
      "coinsurance": 35,
      "copay": 547,
      "penaltyApplies": "Y",
      "penaltyRule": "Non Emergency ER Use",
      "penaltyType": "Coinsurance",
      "penaltyValue": 890,
      "deductibleApplies": "Y"
    }
  ]
}

谁能帮我实现这个目标?

所有其他字段都作为 UI 提交的表单,但 Id 是在后端自动生成的,其值是我想要的表单提交时的值。

【问题讨论】:

    标签: angular


    【解决方案1】:

    订阅获取contractId

    我假设您的请求方法为 GETresponse json 为:

    {
      "contractId": 44,
      "contractName": "BOND",
      "contractServiceList": [
        {
          "id": 44,
          "serviceId": 1,
          "providerTier": null,
          "coinsurance": 35,
          "copay": 547,
          "penaltyApplies": "Y",
          "penaltyRule": "Non Emergency ER Use",
          "penaltyType": "Coinsurance",
          "penaltyValue": 890,
          "deductibleApplies": "Y"
        }
      ]
    }
    

    获取数据:

    服务中:

    import {
        HttpClient,
    } from '@angular/common/http';
    
    constructor(private http: HttpClient) {}
    
    this.http.get('http:localhost:8080/api').subscribe((resp: any) => {
        console.log(resp.contractId)
    });
    

    参考 ===> https://angular.io/guide/http

    【讨论】:

    • 我猜你没有得到我的问题。我提到我没有任何来自 Web API 的直接 GET 方法,但在 Inspect Element 中,它显示为这个 url 包含响应然后我将如何获取它。我猜 http.get 无济于事。
    • 所以据我所知你不知道请求方法是什么?对吗?
    • 不...我在提交表单时只有一个问题,我传递的是合同名称和列表,而不是合同ID,但是当我执行检查元素时,我可以在网络选项卡中看到它整个 json 框架。这是怎么来的? Bcoz 我想从该响应中获取值。我那里没有 http.get。
    • 如果您愿意,我们可以加入聊天以进行澄清。
    • 好的,我需要更多详细信息。
    猜你喜欢
    • 2019-04-05
    • 1970-01-01
    • 2019-08-08
    • 2017-10-24
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2015-07-15
    相关资源
    最近更新 更多