Arrays 离子列表显示异步添加到阵列的数据

Arrays 离子列表显示异步添加到阵列的数据,arrays,asynchronous,events,ionic-framework,Arrays,Asynchronous,Events,Ionic Framework,我是爱奥尼亚的新手。我正在尝试制作一个应用程序来搜索蓝牙设备并显示它们。 我创建了一个搜索蓝牙设备的服务。当设备发现事件时,会发布如下内容: this.event.publish("ibeacon", newBeacon); 以下是我的事件注入: import { Events } from 'ionic-angular/util/events'; ...... constructor(private event: Events){} 以下是我订阅活动以获取发布数据的方式: this.eve

我是爱奥尼亚的新手。我正在尝试制作一个应用程序来搜索蓝牙设备并显示它们。 我创建了一个搜索蓝牙设备的服务。当设备发现事件时,会发布如下内容:

this.event.publish("ibeacon", newBeacon);
以下是我的事件注入:

import { Events } from 'ionic-angular/util/events';
......
constructor(private event: Events){}
以下是我订阅活动以获取发布数据的方式:

this.event.subscribe("ibeacon", (newBeacon) => {
  this.iBeacons.push(newBeacon);
});
如您所见,我已经声明了一个iBeacons数组,在这里我推送接收到的对象。问题是当我试图显示iBeacons数组内容时,没有显示任何内容。以下是显示阵列的方式:

<ion-content padding>

  <ion-list>
   <ion-item *ngFor="let beacon of iBeacons">
     <ion-label>
      {{ beacon.hash }}
     </ion-label>
   </ion-item>
 </ion-list>

</ion-content>
以下是iBeacons阵列初始化:

iBeacons:any [] = [];

尝试如下使用ChangeDetectorRef:

import { ChangeDetectorRef } from '@angular/core';

constructor(private cd: ChangeDetectorRef) {}

this.event.subscribe("ibeacon", (newBeacon) => {
  this.iBeacons.push(newBeacon);
  this.cd.detectChanges();
});

*ngFor每次更新变量时都会更新。所以问题出在别的地方。你能记录数组并将其内容添加到问题中吗?我在问题中添加了一些iBeacons数组包含的内容。谢谢,这看起来不错。不要将数据推送到数组,而是尝试这样做:this.iBeacons=this.iBeacons.concatnewBeacon。我可以想象,如果你不设置变量,它不会触发。仍然没有显示。好的,你是如何初始化变量iBeacons的,你能把它也添加到你的问题中吗?你有没有试着硬编码一些数组来检查它是否显示出来?嘿,谢谢你的工作;但当我转到上一页并再次开始扫描时,我得到了以下错误:未捕获错误:ViewDestroyedError:尝试使用已销毁的视图:DetectChange我找到了答案。我不得不退订这个活动。谢谢您:
import { ChangeDetectorRef } from '@angular/core';

constructor(private cd: ChangeDetectorRef) {}

this.event.subscribe("ibeacon", (newBeacon) => {
  this.iBeacons.push(newBeacon);
  this.cd.detectChanges();
});