如何从有角度的http客户端(角度5)的结果中获取布尔值? [英] How to fetch a boolean value from the result in angular http client (angular 5)?

查看:87
本文介绍了如何从有角度的http客户端(角度5)的结果中获取布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有以下服务呼叫的Angular 5应用程序:

I have Angular 5 app with the following service call:

let isExist: boolean;
this.http.get<Boolean>(`${this.baseUrl}/Trips/TripExist`, {
      headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' }),
      params: {id: id, name: name},
      observe: 'response'
    }).subscribe(    
        data => { isExist = data.body;
          console.log(data);
        },
        err => console. error(err)        
      );
if (isExist == true) {
  Console....
}

其余api如下:

@GET
@Produces("text/plain") 
@Path("TripExist")
public boolean isTripExist(@QueryParam("id") String id,
        @QueryParam("name") String name) {      
    return tripDao.isTripExist(name, id); 
}

我在控制台中获取一个带有正文中布尔值的HttpResponse,但是我不知道如何获取该值并将其分配给布尔值.

I'm getting in the console an HttpResponse with the boolean value in the body but I don't know how to fetch the value and assign it to a boolean value.

推荐答案

我不确定为什么要在其中传递observe选项.我假设您想读取响应上的一些标头或其他一些元数据.记住这一点,既然您已经完成了{ observe: 'response' },您将获得完整的Response对象,其中包含很多字段.但是您只需要关注body字段.

I'm not sure why you're passing the observe option there. I'm assuming that you want to read some headers or some other meta data on the response. Keeping that in mind, since you've done { observe: 'response' }, you'll get the complete Response object with a lot of fields in it. But all you're concerned about is the body field.

因此您可以像这样阅读它:

So you can read it like this:

let isExist: boolean;

this.http.get(`${this.baseUrl}/Trips/TripExist`, {
  headers: new HttpHeaders({
    'Accept': 'text/plain',
    'Content-Type': 'text/plain'
  }),
  params: {
    id: id,
    name: name
  },
  observe: 'response'
}).subscribe(
  data => {
    isExist = data.body; // HERE data.body will have the boolean that you're looking for.
    console.log(data);
    console.log(isExist);
    if (isExist === true) { console.... }
  },
  err => console.error(err)
);

更新:

如果if条件不在subscribe块之外,则它将不起作用.订阅块中的代码异步运行,即在完成API调用并接收到响应之后.但是if条件将同步运行,即在subscribe块之前.因此,当控件达到您的if条件时,isExist变量仍将为undefined,因为它尚未初始化,仅在运行 AFTER subscribe块内进行了初始化. if条件被执行.

UPDATE:

It won't work if the if condition is outside the subscribe block. The code inside the subscribe block runs asynchronously, i.e. after the API call is done with and the response is received. But the if condition would run synchronously, i.e. before the subscribe block. So when the control reaches your if condition, the isExist variable would still be undefined as it hasn't been initialized and only gets initialized inside the subscribe block which runs AFTER the if condition is executed.

我已将if条件移动到subscribe块内,更新了答案.

I've updated my answer with the if condition moved inside the subscribe block.

这篇关于如何从有角度的http客户端(角度5)的结果中获取布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆