【问题标题】:Ionic2 accesing variables in nested methods of cordova pluginIonic2在cordova插件的嵌套方法中访问变量
【发布时间】:2016-06-09 05:03:12
【问题描述】:

我正在使用最新的 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();
    }
 }

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

所以问题是

  1. ionic2 的任何全局变量都可以在任何地方访问吗?
  2. 上述问题的任何其他解决方法?

请指教 -奈蒂克

【问题讨论】:

  • 使用承诺而不是委托。我可能会遗漏一些东西,但没有任何东西可以调用 didRangeBeaconInRegion。您只委托未激活
  • 你能给我代码 sn-p 因为我是 angular2 和 ionic2 的新手!请。
  • 你在哪里导入插件?你能展示你的进口吗?谢谢

标签: cordova angular cordova-plugins ibeacon ionic2


【解决方案1】:

如果您使用箭头符号,您将维护this 上下文:

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

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

编辑:

此外,由于这将在角度区域之外运行,因此您需要将其包装在角度 ngZone 中。

【讨论】:

  • 您好,感谢您的帮助,但它没有更新关联的视图值?像 Meters: {{distance}} 或 请帮忙。
  • 看编辑,需要导入ngzone并注入到构造函数中
猜你喜欢
  • 1970-01-01
  • 2017-01-24
  • 2014-07-31
  • 2016-12-24
  • 2015-08-10
  • 2021-03-05
  • 1970-01-01
  • 2016-11-27
  • 2015-04-17
相关资源
最近更新 更多