我正在制作Angular Project。
test:string;
response:any;
web_assign() {
this.http.get(this.url1).subscribe( e => this.response = e);
this.test = "OK";
this.t=this.response.name2;
return false;}
在此行显示错误。
this.t=this.response.name2;
它说它无法读取'name2'的属性
答案 0 :(得分:1)
您需要在订阅回调中进行分配:
this.http.get(this.url1)
.subscribe( e => {
this.response = e;
this.test = "OK";
this.t=this.response.name2;
});
http.get
是异步的,this.response
仍不会在下一行中定义,但是它将在subscribe
回调中定义。