getMemberInformation() {
let url = "http://abc/abc/xyz";
return this._http.get(url, xhrRequestOptions()).map(
data => {
return JSON.stringify(data);
})
}}
myRequestResponse:any;
ngOnInit() {
this._AnotherClass.getMemberInformation().subscribe(data => console.log('Data Level 0:',data));
this._AnotherClass.getMemberInformation().subscribe(data => this.myRequestResponse = data);
console.log('Data Level 1:', this.myRequestResponse);
=========编辑后,代码数据将出现在数据级0日志中,但数据栏1不显示数据。
答案 0 :(得分:1)
这是设计的。数据仅在函数传递给subscribe(...)
data => this.getMyCarsFromServer = JSON.stringify(data)
被召唤。
只需将代码更改为
即可.subscribe(
data => {
this.getMyCarsFromServer = JSON.stringify(data); // put the data returned from the server in our variable
console.log('hello data', this.getMyCarsFromServer);
},
error => console.log("Error HTTP GET Service"), // in case of failure show this message
() => console.log("Job Done Get !", this.getMyCarsFromServer)//run this code in all cases
);
您可以将.subscribe()
更改为.map()
,然后返回Observable
而不是Subscription
,并且来电者可以订阅。
someMethod() {
return this.someService.someObservable.map(
data => {
//this.getMyCarsFromServer = JSON.stringify(data); // put the data returned from the server in our variable
// console.log('hello data', this.getMyCarsFromServer);
return JSON.stringify(data);
})
.catch(error => {
console.log("Error HTTP GET Service"); // in case of failure show this message
throw error;
// or
return Observable.throw();
// or
return Observable.of([]);
})
.finally(() => console.log("Job Done Get !", this.getMyCarsFromServer);//run this code in all cases
}
this.someMethod().subscribe(data => console.log(data));