2016-05-06 38 views
1

角度2で観測値を正しく使用する方法を理解する上で問題があります。角度2で観測可能なデータを抽出する

let headers = new Headers({ 'Content-Type': 'application/json' }); 
let options = new RequestOptions({ headers: headers }); 
return this.http.get('http://localhost:3000/admin/is-admin.json', options) 
    .map(res => res) 
    .catch(this.handleError); } 

API呼び出しがtrueまたは未定義のブール値を返しますが、私は何とか私の関数からこの値を返すことができないです:私は、私が使用して次のクエリがあります。現時点では、オブジェクトを返します。ここで、ブール値を直接返すようにします。私は私のマップのラインを変更する場合:

.map(res => JSON.parse(res)) 

私は、JSONのパースエラーを取得し、私はそれを変更した場合、:

.map(res => res.data) 

私は、データが応答オブジェクトに存在しないというエラーが出ます。誰が私が何をすべきか知っていますか?私は、応答ペイロードのテキスト値を取得することにより、以下のように使用することになり...

+0

'.map(res => res.json())' – yelsayed

答えて

0

答えは、おそらくかなり基本的なものですが、私は今、かなりの時間のためにそれを探している:

let headers = new Headers({ 'Content-Type': 'application/json' }); 
let options = new RequestOptions({ headers: headers }); 
return this.http.get('http://localhost:3000/admin/is-admin.json', options) 
    .map(res => { // <----- 
    let responsePayload = res.text(); 
    return (responsePayload != null) ? Boolean(res.text()) : responsePayload; 
    })) 
    .catch(this.handleError); 

あなたは可能性があり

let headers = new Headers({ 'Content-Type': 'application/json' }); 
let options = new RequestOptions({ headers: headers }); 
return this.http.get('http://localhost:3000/admin/is-admin.json', options) 
    .map(res => res.json()) // <----- 
    .catch(this.handleError); 
0

あなたが観察

getData() { 
    let headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 
    return this.http.get('http://localhost:3000/admin/is-admin.json', options) 
    .map(res => res.json) 
    .catch(this.handleError); 
} 

otherFunc() { 
    this.getData().subscribe(val => this.data = val); 
} 
に加入する必要があります。また、 jsonメソッドを呼び出そう

又は

<div> 
    {{data | json}} 
</div> 

コンストラクタ(){ this.data =のgetData()。 }

関連する問題