【发布时间】:2017-12-22 08:19:55
【问题描述】:
如何将接收到的数据写入tmp?通过return,函数返回promise对象,在函数tmp中是不可见的
tmp: string;
constructor(private http: Http) {
this.tmp = "load";
this.GetUsers();
}
ngOnInit() {
setTimeout(console.log("Hello"), 2000);
}
GetUsers() {
this.http.get('http://localhost:1337/api/users')
.toPromise()
.then(function(response) {
this.tmp = "success"
})
.catch(this.handleError);
另外,setTimeout 也不起作用。也就是说,它只工作一次。
constructor(private http: Http) {
this.tmp = "load";
this.GetUsers();
}
ngOnInit() {
}
GetUsers() {
setTimeout(console.log("Hello"), 2000);
}
【问题讨论】:
-
“记录”是什么意思?
-
@Flaugzig 写
-
setTimeout 按设计执行一次。另请参阅this 页面。
-
我还是不明白你的问题。是否要从响应中提取数据?
-
@Flaugzig 我想知道如何获取数据并将其写入正确的变量。如果问了一个不清楚的问题,我很抱歉
标签: javascript angular promise angular-promise angular-http