私が観察したものについてほとんどすべてを読んだ後、彼らはどのように動作するかはまだかなり分かりません。Ionic2、Angular2、HTTPおよびObservables
私はここにhttpリクエストをしています:
import { Component, OnInit, Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { NavController } from 'ionic-angular';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
webs: any;
getWebs(): any{
return this.http.get('here the url')
.map((res: Response) => res.json());
}
constructor(public navCtrl: NavController, private http: Http) {}
ngOnInit(){
this.getWebs().subscribe(response => {
this.webs = response;
console.log(this.webs);
});
}
}
をコンソールで、this.websが正しく印刷されます。つまり、get要求がうまく動作していて、私が望むオブジェクトを取得しようとしています。これは通常のJSONオブジェクトです。
:問題は、私はその
{{ webs.name }}
のようなオブジェクト(私はコンソールに表示同じプロパティ)のいくつかのプロパティを印刷しようとした場合、私は全体の時間そのエラーを取得し、ビューに、です私はすでにチュートリアルをたくさん読んで、私は私の問題への答えを見つけることができません。あなたの助けを
感謝。角1 :(とすっごく簡単だった
Error in ./HomePage class HomePage - caused by: Cannot read property 'name' of undefined
{{webs?.name}}とthis.web = getWebs()を使用しています。ありがとうございます! – antsanchez