2017-05-12 7 views
0

ビーコンイベントをリッスンしているページがあります。私はビーコンが検出されたときにポップアップを表示したい。問題は、私はビーコンを検出したときに、div要素が表示されていないということです角2 Observableからのデータバインディング

home.ts

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> 

:私は、次のコードを持っています。私は非同期データバインディングについて読んでいますが、どうやってそれを行うのか分かりません。

誰かが解決する方法を知っていますか?

ありがとうございます。

答えて

1

私はそれはあなたがrxjs BehaviorSubject使うべき場所です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(); 
    }) 
    } 
} 
0

を使用して作業しました:(どこNgZoneが注入され、beaconFoundSubjectを通知するために使用される必要があるかもしれないあなたのコンストラクタで

beaconFoundSubject : BehaviorSubject<boolean> = new BehaviorSubject(false); 

を):

constructor(private ngzone: NgZone) { 

.... 

delegate.didEnterRegion() 
.subscribe(
    (data) => { 
    this.ngzone.run(() => this.beaconFoundSubject.next(true)); 
    } 
) 

テンプレート内:

<div class="card-beacon" *ngIf="beaconFoundSubject | async"> 
</div> 
+0

こんにちは@Peter、あなたのコードを試しましたが、動作しません。私がタブと戻り値を変更したときのみ表示されます。 – Morris

+0

@Morrisをお試しいただきありがとうございます。このコードを取り入れた私のプロジェクトでもう一度見たところ、ngzoneも使用していたことがわかりました。これはおそらく、タブを変更してリターンするまで変更が取得されていないことを経験する理由です。 –

関連する問題