简体   繁体   English

Cordova插件的嵌套方法中的Ionic2访问变量

[英]Ionic2 accesing variables in nested methods of cordova plugin

I am developing mobile app with ionic2(beta 8,Cordova 6.2) latest and facing issue to accessing variable in cordova mehtod(Functions).My code is like 我正在使用最新的ionic2(beta 8,Cordova 6.2)开发移动应用程序,并面临在cordova mehtod(Functions)中访问变量的问题。我的代码就像

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();
    }
 }

Using this plugin for beacon detection 1) https://github.com/petermetz/cordova-plugin-ibeacon/ 使用此插件进行信标检测1) https://github.com/petermetz/cordova-plugin-ibeacon/

So Questions are 所以问题是

  1. Any global variable for ionic2 that can accessible anywhere? ionic2的任何全局变量都可以在任何地方访问?
  2. Any other workaround of above given issue? 上述问题还有其他解决方法吗?

Please Advice -Naitik 请咨询-Naitik

If you use arrow notation you will maintain this context: 如果使用箭头符号,则将维护this上下文:

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

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

Edit: 编辑:

Additionally, since this will run outside the angular zone, you need to wrap it within an angular ngZone. 另外,由于这会在角度区域之外运行,因此您需要将其包装在角度ngZone中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM