【发布时间】:2020-12-13 10:14:36
【问题描述】:
我试图从 subscribe 的布尔值 true/false 中获取价值,但我无法获取价值,这是我目前使用的。
我的 http post 的旧方法。
this.http.post(HostedPathConst.HostedPath + `CompanyProfile/UpdateCustomersStatus`, fData)
.subscribe(data => this.Success(data), err => this.Error(err));
this.Success(data) 和 this.Error(err) 是现在调用的方法,我尝试获取数据的新方法是这样。
const fData = new FormData();
fData.append('Id', Id.toString());
fData.append('IsActive', IsActive.toString());
const test = this.http.post(HostedPathConst.HostedPath + `CompanyProfile/UpdateCustomersStatus`, fData)
.subscribe(data => {
this.Success(data);
this.Switch = IsActive;
return this.Switch;
},
err => {
this.Error(err);
this.Switch = !IsActive;
return this.Switch;
});
console.log(test);//Subscriber {closed: false, _parentOrParents: null, _subscriptions: Array(1), syncErrorValue: null, syncErrorThrown: false, …}
console.log(this.Switch);//undefined at first then gives opposite result like true gives false and vice versa
作为 this.Switch = IsActive; 获取数据的布尔值,它首先返回 undefined,然后返回值 false 而不是 true反之亦然。
【问题讨论】:
-
我并没有真正理解这里的问题,所以我知道您正在发布帖子的服务器的响应返回一个布尔值,但这意味着它可以正常工作并且您可以获得响应来自服务器。
-
@Erik 我的布尔值是一个复选框,如果选中它会返回 true,否则返回 false,但在第一种情况下,如果我在 console.log 上选中它的 false 并取消选中它的 true,它会返回 undefined 然后相反的值
-
嗯,有一件事是肯定的。 Http 操作是异步的,所以在执行时有可能,这个 console.log(this.Switch);会在这之前发生,this.Switch = IsActive;所以也许,这就是你变得不确定的原因。
-
@Erik 我如何使用同步的例子会有帮助..?
标签: angular typescript angular8 angular-http