2016-06-09 5 views
0

Ich entwickle mobile app mit ionic2 (beta 8, Cordova 6.2) neuesten und mit Blick auf Ausgabe Variable in cordova Mehtod für den Zugriff (Funktionen) .My Code ist wieIonic2 accesing Variablen in verschachtelten Methoden der cordova Plugin

import {Page, NavController, Alert, NavParams} from 'ionic-angular'; 
import {StatusBar,cordovaBeacon} from 'ionic-native'; 
import {AuthService} from '../home/authservice'; 
import {HomePage} from '../home/home'; 


export class UserPage { 
    constructor(authservice, navcontroller) { 
     this.service = authservice; 
     this.nav = navcontroller; 
     this.distance = 0;  
    } 

    getDistance(){    
     this.distance=-50;//This is working and change view perfactly 

     var delegate = new cordova.plugins.locationManager.Delegate(); 

     delegate.didRangeBeaconsInRegion = function (pluginResult) { 
      //I got reading of beacons but can't access distance variable to change distance in associate view 
      this.distance=pluginResult.beacons[0].rssi; 
      /*This wan't work, can't access this.distance variable   to update(View) proximity of ibeacon in delegete method*/ 
     }; 

     var uuid = '33333333-3333-4444-5555-666666666666 '; 
     var identifier = 'BEacon 2'; 
     var minor = '1'; 
     var major = '1'; 

     var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor); 

     cordova.plugins.locationManager.setDelegate(delegate); 
     cordova.plugins.locationManager.requestWhenInUseAuthorization(); 
     cordova.plugins.locationManager.requestAlwaysAuthorization(); 

     cordova.plugins.locationManager.startRangingBeaconsInRegion(beaconRegion) 
      .fail(function(e) { console.error(e); }) 
      .done(); 
    } 
} 

Mit dieser Plugin für Leuchtfeuer Erkennung 1) https://github.com/petermetz/cordova-plugin-ibeacon/

So Fragen sind

  1. Jede globale Variable für ionische 2 das kann überall zugänglich sein?
  2. Jede andere Problemumgehung des oben angegebenen Problems?

Bitte Beratung -Naitik

+0

Verwenden Sie Versprechen statt Delegierten. Ich vermisse etwas, aber es gibt nichts, das didRangeBeaconInRegion aufruft. Du delegierst nur nicht aktiviert – misha130

+0

Kannst du mir bitte Code-Snippet geben, weil ich neu bei angular2 und ionic2 bin! Bitte. – user2617214

+1

Wo importieren Sie das Plugin? können Sie Ihre Importe als auch zeigen, danke – brianlmerritt

Antwort

1

Wenn Sie Pfeil-Notation verwenden Sie this Kontext beibehalten wird:

constructor(authservice, navcontroller, ngzone) {...} 

delegate.didRangeBeaconsInRegion = (pluginResult) => { 
      this.ngzone.run(() => { 
       this.distance=pluginResult.beacons[0].rssi;   
      }); 
     }; 

Edit:

Zusätzlich kann, da dies außerhalb der läuft Winkelzone, müssen Sie es in einem angular ngZone wickeln.

+0

Hallo, Danke für Ihre Hilfe, aber es aktualisiert nicht verbundenen Ansichtswert? Wie Meter: {{distance}} oder Bitte helfen. – user2617214

+0

Blick auf die Bearbeitung, müssen Sie ngzone importieren und injizieren Sie es im Konstruktor –

Verwandte Themen