2016-09-12 4 views
1

私は最初のAngular2(rc.6)プロジェクトを開始しています。 JSONオブジェクトがコンポーネントに正常に送信されましたが、テンプレートのキー値にアクセスできません。Angular2(rc.6)JSONオブジェクトキーにアクセスできません

SERVICE(抜粋):

@Injectable() 
export class SongService { 
    constructor(private http: Http) { } 

    getSong(id: number): Promise<Song> { 
    let url = '/maxapirest/v1/maxmusic/song/'+id 
    console.log(url) 
    return this.http 
     .get(url) 
     .toPromise() 
     .then(function(response) { 
      console.log(response.json()); 
      return response.json(); 
     }) 
    } 

COMPONENT(抜粋):

@Component({ 
    selector: 'my-song-reading', 
    templateUrl: STATIC_URL+'song-reading.component.html', 
    providers: [ SongService ], 
}) 

export class SongReadingComponent implements OnInit { 
    song: Promise<Song>; 
    constructor(
    private songService: SongService, 
    private route: ActivatedRoute) { } 

    ngOnInit(): void { 
    this.route.params.forEach((params: Params) => { 
     if (params['id'] !== undefined) { 
     let id = +params['id']; 

     this.song = this.songService.getSong(id) 
     } 
    }); 

    } 

TEMPLATE(抜粋):

<div *ngIf="song"> 
    {{ song | async | json }}<br/><br/><br/> 
    {{ song.title | async}} 
    {{ song.image | async }} 
    {{ song.id | async}} 
</div> 

私は理解できない問題それは{{歌| json}}はJSONオブジェクトを正しく出力します。 {"id":71、 "title": "それは意味しません" ...} エラーはスローされません。 しかし、他のvarキーはレンダリングされません。

アイデア?

+0

あなたは何を得るのですか 'はconsole.log(this.song)'に確認してください? – micronyks

答えて

1

あなたは.then(...)を使用して、そこに値を代入する必要があります。

ngOnInit(): void { 
    this.route.params.forEach((params: Params) => { 
     if (params['id'] !== undefined) { 
     let id = +params['id']; 

     this.songService.getSong(id) 
     .then(json => { 
      this.song = json; 
     }); 
     } 
    }); 
    } 
+1

正確に。ありがとう!私は約束のサイクルを考慮していなかった...もちろん、私はまた曲を変更する必要があった:約束;歌へ:{}; – Cruclax

関連する問題