2016-06-29 13 views
5

bir mantıksal değeri:angularjs 2 ZoneAwarePromise Bu yöntem ile API verileri ile restClient için angularjs 2'deki bir temel sınıf

export class InfoDataBoolean { 
    public data: boolean; 
    public error: string; 
} 
: InfoDataBoolean iki özelliğe sahip bir sınıftır

public getInfoDataBooleanRequest(url: string): InfoDataBoolean { 
    return this.http.get(this.urlApiPrefix + url, this.createRequestOptions()) 
     .toPromise() 
     .then(response => <InfoDataBoolean>response.json()) 
     .catch(this.handleError); 
} 

Hizmet yöntemimi aradığım başka bir sınıf var. Bu çağrı, InfoDataBoolean'daki verileri döndürmek istediğim bir yöntem içinde, bunun gibi InfoDataBoolean sınıfının değil.

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()) çıktısı:

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

Ama benim yöntemiyle isLogged() den true veya false dönmek istiyorum.

Bunu nasıl yapabilirim?

cevap

9

isLogged yönteminizin senkronize olmadığını ve bir söz verdiğinizi unutmayın. sonuç almak için then yöntemiyle üzerinde bir geri arama kaydetmeniz gerekir: Bu ne zaman çözüleceğine Senin durumunda

console.log(isLogged()); 
isLogged().then(data => { 
    console.log(data); 
}); 

, sen sözünü ve dönen sonucu görüntülemek ...