我在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]}
但是,我想回到true或false从我的方法isLogged().
我怎样才能做到这一点?
Thi*_*ier 15
不要忘记您的isLogged方法是异步的并返回一个promise.要获得结果,您需要使用以下then方法在其上注册回调:
console.log(isLogged());
isLogged().then(data => {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您将在解析时显示承诺和返回的结果...
| 归档时间: |
|
| 查看次数: |
11240 次 |
| 最近记录: |