Ionic2在cordova插件的嵌套方法中访问变量
Ionic2 accesing variables in nested methods of cordova plugin
我正在使用 ionic2(beta 8,Cordova 6.2)开发移动应用程序,并且面临在 cordova mehtod(函数)中访问变量的问题。我的代码就像
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/
所以问题是
- ionic2 有任何可以在任何地方访问的全局变量吗?
- 上述给定问题的任何其他解决方法?
请指教
-奈蒂克
如果您使用箭头符号,您将保持 this
上下文:
constructor(authservice, navcontroller, ngzone) {...}
delegate.didRangeBeaconsInRegion = (pluginResult) => {
this.ngzone.run( () => {
this.distance=pluginResult.beacons[0].rssi;
});
};
编辑:
此外,由于这会 运行 在 angular 区域之外,您需要将其包装在 angular ngZone 中。
我正在使用 ionic2(beta 8,Cordova 6.2)开发移动应用程序,并且面临在 cordova mehtod(函数)中访问变量的问题。我的代码就像
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/
所以问题是
- ionic2 有任何可以在任何地方访问的全局变量吗?
- 上述给定问题的任何其他解决方法?
请指教 -奈蒂克
如果您使用箭头符号,您将保持 this
上下文:
constructor(authservice, navcontroller, ngzone) {...}
delegate.didRangeBeaconsInRegion = (pluginResult) => {
this.ngzone.run( () => {
this.distance=pluginResult.beacons[0].rssi;
});
};
编辑:
此外,由于这会 运行 在 angular 区域之外,您需要将其包装在 angular ngZone 中。