2016-06-29 12 views
5

mam klasy bazowej w angularjs 2 dla restClient do transmisji danych, z automatycznie z tym sposobem:angularjs 2 ZoneAwarePromise zamiast wartość logiczną

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

gdzie InfoDataBoolean jest klasa dwie właściwości:

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

Mam inną klasę, w której nazwałem moją metodę obsługi. To wywołanie znajduje się wewnątrz metody, w której chcę zwrócić dane z InfoDataBoolean, a nie klasy InfoDataBoolean w ten sposób.

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; 
    }); 
} 

Wyjście console.log(isLogged()):

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

Ale chcę wrócić true lub false z mojego sposobu isLogged().

Jak mogę to zrobić?

Odpowiedz

9

Nie zapominaj, że twoja metoda isLogged jest asynchroniczna i zwraca obietnicę. Aby uzyskać wynik trzeba zarejestrować wywołania zwrotnego na nim metody then:

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

W twoim przypadku, wyświetlanie obietnicę i zwrócony wynik, gdy zostanie on rozwiązany ...

Powiązane problemy