加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS 2 ZoneAwarePromise改为布尔值

发布时间:2020-12-17 10:25:48 所属栏目:安全 来源:网络整理
导读:我在AngularJS 2中有一个基类,restClient使用这个方法从API调用数据: public getInfoDataBooleanRequest(url: string): InfoDataBoolean { return this.http.get(this.urlApiPrefix + url,this.createRequestOptions()) .toPromise() .then(response = Info
我在AngularJS 2中有一个基类,restClient使用这个方法从API调用数据:
public getInfoDataBooleanRequest(url: string): InfoDataBoolean {
  return this.http.get(this.urlApiPrefix + url,this.createRequestOptions())
      .toPromise()
      .then(response => <InfoDataBoolean>response.json())
      .catch(this.handleError);
}

其中InfoDataBoolean是一个具有两个属性的类:

export class InfoDataBoolean {
    public data: boolean;
    public error: string;
}

我有另一个课,我打电话给我的服务方法.
这个调用在一个方法里面,我想从InfoDataBoolean返回数据,而不是像这样的类InfoDataBoolean.

public isLogged(): boolean {
   return this.getInfoDataBooleanRequest('islogged').then(x => {
      let result: InfoDataBoolean = x;

      if(result.error !== "1") {
        console.log('Success is failed');
        return false;
      }

      return result.data;
   });
}

console.log的输出(isLogged()):

ZoneAwarePromise {__zone_symbol__state: null,__zone_symbol__value: Array[0]}

但我希望从我的方法isLogged()返回true或false.

我怎样才能做到这一点?

不要忘记您的isLogged方法是异步的并返回一个promise.要获得结果,您需要使用then方法在其上注册回调:
console.log(isLogged());
isLogged().then(data => {
  console.log(data);
});

在您的情况下,您将在解析时显示承诺和返回的结果…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读