SAP WebIDE 未捕获引用错误:o未定义视图



我正在尝试在SAP WebIDE中使用Geolocation Cordova插件。科尔多瓦插件运行完美,我可以获取经度和经度信息。问题出在给我错误的这一行上:oView 未定义。

oView.byId("txtLatitude").setText(position.coords.latitude);

未捕获的引用错误:o未定义视图

在XML视图中,我设置了txtLatitude id,因此XML视图没有问题。

这是控制器的代码:

sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";
return Controller.extend("GPS.GPS.controller.GPS", {
oView: null,
onInit: function() {
var oView = this.getView();
},
getPosition: function() {
navigator.geolocation.getCurrentPosition(this.onGeoSuccess, this.onGeoError, {
enableHighAccuracy: true
});
},
onGeoSuccess: function(position) {
oView.byId("txtLatitude").setText(position.coords.latitude); <<--This line got error: undefined
},

onGeoError: function() {
console.log('code: ' + error.code + 'n' + 'message: ' + error.message + 'n');
},      
});
});

如何解决这个问题?

问题出在onGeoSuccess中的函数作用域上,因为它是一个回调(这就是this.getView()不起作用的原因(。

处理此问题的一种方法是使用 bind((:

navigator.geolocation.getCurrentPosition(this.onGeoSuccess.bind(this), ...

然后你可以在里面使用this.getView()...onGeoSuccess.

最新更新