小编Séb*_*ien的帖子

AngularJS 2 ZoneAwarePromise改为布尔值

我在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);
}
Run Code Online (Sandbox Code Playgroud)

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

export class InfoDataBoolean {
    public data: boolean;
    public error: string;
}
Run Code Online (Sandbox Code Playgroud)

我有另一个课,我打电话给我的服务方法.这个调用在一个方法里面,我想从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;
   });
}
Run Code Online (Sandbox Code Playgroud)

输出console.log(isLogged()):

ZoneAwarePromise {__ zone_symbol__state:null,__ zone_symbol__value:Array [0]}

但是,我想回到truefalse从我的方法isLogged().

我怎样才能做到这一点?

angular

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×1