数组未在Angular中的subscribe()中填充

时间:2017-11-17 00:07:33

标签: angular typescript

onButtonClick() {
this.http.get('ip:port/abc')
.subscribe(res => {
  console.log(res);
  [].push.apply(this.dummy, res);
  console.log(this.dummy);
});
}

当我检查控制台时,res对象正在打印如下:

{lat: 29, lon: 82, label: "A", draggable: true}

但是当我用res对象填充虚拟数组时,它不会被填充。在控制台上打印它会显示一个长度为零的数组。

1 个答案:

答案 0 :(得分:0)

像这样使用:

dummy : any = [];
onButtonClick() {
     this.http.get('ip:port/abc')
     .subscribe(res => {
     console.log(res);
     this.dummy.push(res);
     console.log(this.dummy);
  });
}