【发布时间】:2018-07-18 01:20:13
【问题描述】:
对不起我的英语。
我在尝试使用 subscribe 或 Promise 查看 api rest 的结果时遇到问题。
我有一个提供下一个代码的提供程序:
提供者:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class UserServiceProvider {
path : string = 'my_paht_remote';
constructor(public http: HttpClient) {
console.log('Hello UserServiceProvider Provider');
}
getUsers() {
return this.http.get(this.path');
}
getList()
{
return new Promise ( resolve => {
this.http.get(this.path).subscribe(
data => {
resolve(data);
}, err => {
console.error();
}
);
} );
}
}
现在,在文件 typeScript 中:
--Imports
...
import { UserServiceProvider } from '../../providers/user-service/user-service';
...
export class CaicesPage {
users: any;
users2: any;
...
ionViewDidLoad()
{
this.showMap(); //This is a function for see Google Maps.
}
showMap()
{
//Here I can see the result of JSon File remote:
this.userService.getUsers().subscribe(
(data) => { // Success
this.users = data['results'];
},
(error) =>{
console.error(error);
}
);
console.info(this.users); // I see Undefined, Why ?
//Whith the next form, Also can see the content File Json
this.userService.getList().then(
data => {
this.users2=data['results'];
}
);
console.info(this.users2); // I see Undefined, Why ?
}
}
如何在订阅或 THEN 之外查看 var users 或 var users2 的值?
感谢合作
【问题讨论】:
标签: typescript ionic-framework ionic3