2016-02-20 15 views
5

私のAPIから情報を返そうとしていますが、正しく購読する方法を理解できません。配列を使用して、私のサービスから空の配列を返し、値を取得します。TypeScript Angular 2 Responses Subscription

app-component tsで1つの値の変数を正しく返すにはどうすればよいですか。今私はalert(JSON.stringify(authenticated))を行う場合、それはちょうど私{"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}

アプリ成分TS

checkAuthentication(){ 
    var authenticated = this._authService.getAuthenication(); 
} 

認証サービス

getAuthenication() { 
     return this._http.get('auth.php').subscribe(res => { 
     if (res.json().authenticated === true){ 
      return true; 
     } 
     return false; 
     }); 
    } 

auth.php

<? 
session_start(); 
$authenticated = (isset($_SESSION["authenticated"]) ? true : false); 
$post_data = json_encode(array(authenticated => $authenticated)); 
echo $post_data; 
?> 

答えて

7

変更

を与えます
return this._http.get('auth.php').subscribe(res => { 

return this._http.get('auth.php').map(res => { 

と私はタイプ '購読 ' に存在しません '購読' は、プロパティを取得しています

this._authService.getAuthenication() 
    .subscribe(value => this.authenticated = value); 
+0

のようにそれを呼び出すこと。角またはjavascriptを購読していますが、それに関するドキュメントはありますか? – ClickThisNick

+1

申し訳ありません、変更を忘れました –

+0

認証されたものは.subscribe(value => this.authenticated = authenticated)で参照されます。それはapp.component上にある変数にありますか、それとも価値のあるプロパティですか?私はマップ内でtrueまたはfalseを返しています。res.json()を返すべきですか? – ClickThisNick

関連する問題