离子列表显示异步添加到数组的数据 [英] ion-list display asynchronously added data to array
问题描述
我是离子领域的新手。我试图制作一个可以搜索蓝牙设备并显示它们的应用程序。
我创建了一个搜索蓝牙设备的服务。当设备发现事件发布时,如下所示:
I am new in ionic. I trying to make an app which will search for Bluetooth devices and will display them. I have created a service which search for Bluetooth devices. When a device found an event publishes like below:
this.event.publish("ibeacon", newBeacon);
这是我的事件注入:
import { Events } from 'ionic-angular/util/events';
......
constructor(private event: Events){}
这是我订阅活动以获取已发布数据的方式:
Here is how I subscribe to the event in order to get the published data:
this.event.subscribe("ibeacon", (newBeacon) => {
this.iBeacons.push(newBeacon);
});
如您所见,我声明了一个iBeacons数组,将接收到的对象放入其中。问题是,当我尝试不显示iBeacons Array内容时,什么也没显示。这是显示数组的方式:
As you can see I have declared an iBeacons Array where I push my received objects. The problem is when I try to display the iBeacons Array contents nothing displayed. Here is how I display my array:
<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数组中的。知道吗?
The array at the beginning is empty. I have have checked and I receive correctly my devices when I subscribe to the event. I don't get errors. I thing the data is not displayed because the data is added asynchronously in iBeacons array. Any idea?
以下是一些iBeacons数组,其中包含:
Here are some of the iBeacons Array contains:
(4) [{…}, {…}, {…}, {…}]
0: {hash: "b5d787ac973341a59bf73838eededcb4", uuid: "fda50693-a4e2-4fb1-afcf-c6eb07647825", major: "10001", minor: "10301", lastFoundTime: 1518003454401}
1: {hash: "50aee081c9833e51ce00b9aa4a0c062d", uuid: "fda50693-a4e2-4fb1-afcf-c6eb07647825", major: "10001", minor: "10206", lastFoundTime: 1518003454391}
2: {hash: "1c8ecafb6efbbc37f905d95551291672", uuid: "fda50693-a4e2-4fb1-afcf-c6eb07647825", major: "10001", minor: "10208", lastFoundTime: 1518003454391}
3: {hash: "442e383d9c582985083b5b05f07161d2", uuid: "fda50693-a4e2-4fb1-afcf-c6eb07647825", major: "10001", minor: "10205", lastFoundTime: 1518003454392}
length: 4
__proto__: Array(0)
这是iBeacons数组初始化:
Here is the iBeacons array initialization:
iBeacons:any [] = [];
推荐答案
尝试像这样使用ChangeDetectorRef:
Try to use ChangeDetectorRef like this:
import { ChangeDetectorRef } from '@angular/core';
constructor(private cd: ChangeDetectorRef) {}
this.event.subscribe("ibeacon", (newBeacon) => {
this.iBeacons.push(newBeacon);
this.cd.detectChanges();
});
这篇关于离子列表显示异步添加到数组的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!