2017-01-11 13 views
-1

私は運がない応答からデータを抽出しようとしています。問題がどこにあるのか教えていただけますか?角2は応答からデータを抽出します

this.tite = data.result[0].LinkID; 
this.tite = data.result[1].LinkName; 

component.html

<div class="team"> 
    <h3>{{title}}</h3> 
<div> 

オブジェクト

getLinks{"result":{"LinkID":1,"LinkName":"Financial Planning Reports Admin"}} 

component.ts

import { Component, OnInit } from '@angular/core'; 
import { NavMenuService } from './navmenu/navMenu.service'; 
//import { ITitle } from './app'; 

@Component({ 
    selector: 'pm-app', 
    moduleId: module.id, 
    templateUrl: 'app.component.html', 
    providers: [NavMenuService] 
}) 
export class AppComponent implements OnInit { 
    pageTitle: any[]; 
    tite: string; 
    titileID: number; 
    errorMessage: string; 
    constructor(private _navMenuService: NavMenuService) { 

    } 

    ngOnInit(): void { 
     this._navMenuService.getLinkName(600860) 
      .subscribe(
      data => { 
       this.tite = data.result[0].LinkID; 
       this.tite = data.result[1].LinkName; 

      }, 
      error => this.errorMessage = <any>error); 

    } 

} 

service.ts

getLinkName(LinkID: number) { 

     return this._http.get(this._fileUploadAPI + 'GetLinks/'+ LinkID.toString()) 
      .map((response: Response) => response.json()) 
      .do(data => console.log('getLinks' + JSON.stringify(data))) 
      .catch(this.handleError); 

    } 
+0

私はthis.titleID = data.result [0] .LinkIDを書きます。 this.tite = data.result [1] .LinkName; – rgoal

+0

error:core.umd.js:2838 EXCEPTION:プロパティ 'LinkID'が未定義です。 core.umd.js:2838 EXCEPTION:私はngOnInit(にそれを変更した場合、未定義 – rgoal

+0

のプロパティ 'のLinkName' を読み取ることができません):無効{ this._navMenuService.getLinkName(600860) .subscribe( データ=> { これ。タイトル= data.result [0]; this.titleID = data.result [1]; => this.errorMessage = エラー }、 エラー); }エラーは表示されませんが、タイトルは – rgoal

答えて

1

サービスから返されるデータの結果プロパティは、上記のコンソール出力に基づく配列ではなく(オブジェクトではなく)配列です。インデックスを削除し、それが動作するはずです。

this.titleID = data.result.LinkID; 
this.title = data.result.LinkName; 
+0

ありがとうございました。 – rgoal

関連する問題