Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在角度输出信息中通过;得到“;?_Javascript_Angular_Promise_Angular Promise_Angular Http - Fatal编程技术网

Javascript 如何在角度输出信息中通过;得到“;?

Javascript 如何在角度输出信息中通过;得到“;?,javascript,angular,promise,angular-promise,angular-http,Javascript,Angular,Promise,Angular Promise,Angular Http,如何将接收到的数据写入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:13

如何将接收到的数据写入
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);
}
试试这个:-

@angular/http

this.http.get('http://localhost:1337/api/users') 
      .toPromise() 
      .then((response:Response)=> { this.tmp = response.json() }) 
      .catch(this.handleError);

您在其中编写了
setTimeout()
ngOnInit()
生命周期钩子在构造函数之后只执行一次。如果编写的代码在组件中,那么它将在每次创建组件对象时执行,但如果它在服务中,那么它将在您提供服务时执行一次,因为服务对象是单例对象

您所说的“记录”是什么意思?@Flaugzig writesetTimeout按设计执行一次。另见第页。我仍然不明白你的问题。您想从响应中提取数据吗?@Flaugzig我想知道如何获取数据并将其写入正确的变量。如果问到一个不清楚的问题,我很抱歉。谢谢
this.http.get
。setTimeout在另一个函数中也不起作用。您能告诉我您在哪里添加setTimeout()吗?它与
setTimeout()无关。
。它是关于你在哪里使用它。如果您愿意让我为您想要的目的使用
setTimeout()
,那么我可能可以帮助您。function
GetUsers
对不起,我找不到您。你能修改你的代码吗?你是如何尝试的。