Ionic2 角度2从可观测数据绑定
我有一个网页是听灯塔事件。我想在检测到信标时显示一个弹出窗口。我有以下代码: home.tsIonic2 角度2从可观测数据绑定,ionic2,angular2-template,ionic3,Ionic2,Angular2 Template,Ionic3,我有一个网页是听灯塔事件。我想在检测到信标时显示一个弹出窗口。我有以下代码: home.ts export class HomePage { beacon_found: boolean; constructor(public navCtrl: NavController, public events: Events, public ibeacon: IBeacon) { this.ibeacon.requestAlwaysAuthorization(); let de
export class HomePage {
beacon_found: boolean;
constructor(public navCtrl: NavController, public events: Events, public ibeacon: IBeacon) {
this.ibeacon.requestAlwaysAuthorization();
let delegate = this.ibeacon.Delegate();
let region = this.ibeacon.BeaconRegion('ibirapuera','B9407F30-F5F8-466E-AFF9-25556B57FE6D');
this.ibeacon.startMonitoringForRegion(region)
.then(
() => console.log('Native layer recieved the request to monitoring'),
error => console.error('Native layer failed to begin monitoring: ', error)
)
delegate.didStartMonitoringForRegion()
.subscribe(
(data) => console.log("Started monitoring beacons", data)
)
delegate.didEnterRegion()
.subscribe(
(data) => {
this.beacon_found = true;
}
)
delegate.didExitRegion()
.subscribe(
(data) => {
console.log("Exit Region");
}
)
}
}
home.html
<div class="card-beacon" *ngIf="beacon_found">
</div>
问题是,当我检测到信标时,div没有显示。我读了一些关于异步数据编址的书,但我不知道如何做
请问有人知道怎么解决吗
提前谢谢。我使用ChangeDetectorRef让它工作起来
import { Component, Input, ChangeDetectorRef } from '@angular/core';
export class HomePage {
beacon_found: boolean;
constructor(public navCtrl: NavController, public events: Events, public cdr: ChangeDetectorRef) {
events.subscribe('beacon:detected',(data) => {
this.beacon_found = true;
cdr.detectChanges();
})
events.subscribe('beacon:undetected',(data) => {
this.beacon_found = false;
cdr.detectChanges();
})
}
}
这是您应该使用rxjs BehaviorSubject的地方:
beaconFoundSubject : BehaviorSubject<boolean> = new BehaviorSubject(false);
然后在模板中:
<div class="card-beacon" *ngIf="beaconFoundSubject | async">
</div>
你好@Peter,我试过你的代码,但不起作用。它只在我更改标签并返回时显示。感谢您尝试@Morris。再次查看我的项目,我从中获取了这段代码,发现我也使用了ngzone,这可能是您体验到更改在更改选项卡并返回之前不会被拾取的原因。
<div class="card-beacon" *ngIf="beaconFoundSubject | async">
</div>