【问题标题】:Nativescript Angular: cannot read property of undefinedNativescript Angular:无法读取未定义的属性
【发布时间】:2016-07-26 08:48:52
【问题描述】:

我有以下代码绑定到latitudelongitude。我希望我的地理位置跟踪器更新这些值。但是 watchPosition 看不到 this.latitudethis.longitude

@Component({
  selector: "my-app",
  templateUrl: "app.component.html",
})
export class AppComponent {

latitude: number;
longitude: number;
watchId = 0;

constructor() {
    this.latitude = 0;
    this.longitude = 0;
}

public startTracking() {

    /* Get Location */
    this.watchId = geolocation.watchLocation(function (loc) {
            if (loc) {
                console.log("Current location is: " + loc.latitude + ", " + loc.longitude);
                this.latitude = loc.latitude;
                this.longitude = loc.longitude;
            }
        }, function (e) {
            console.log("Error: " + e.message);
        },
        {desiredAccuracy: enums.Accuracy.any, updateDistance: 10, minimumUpdateTime: 1000});
}

public stopTracking() {
    if (this.watchId) {
        geolocation.clearWatch(this.watchId);
    }
}

}

这是我得到的错误

com.tns.NativeScriptException: 
Calling js method onLocationChanged failed

TypeError: Cannot set property 'latitude' of undefined
File: "/data/data/org.nativescript.elektra/files/app/app.component.js, line: 44, column: 30

StackTrace: 
Frame: function:'', file:'/data/data/org.nativescript.elektra/files/app/app.component.js', line: 44, column: 31
Frame: function:'android.location.LocationListener.onLocationChanged', file:'/data/data/org.nativescript.elektra/files/app/tns_modules/nativescript-geolocation/nativescript-geolocation.js', line: 25, column: 17


at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:861)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:726)
at com.tns.Runtime.callJSMethod(Runtime.java:712)
at com.tns.Runtime.callJSMethod(Runtime.java:693)
at com.tns.Runtime.callJSMethod(Runtime.java:683)
at com.tns.gen.android.location.LocationListener.onLocationChanged(LocationListener.java:11)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: angular2-nativescript


    【解决方案1】:

    这是因为this匿名函数中是不可见的。在启动匿名函数之前,您必须存储this 的引用。

    public startTracking() {
    let self = this;
    /* Get Location */
    this.watchId = geolocation.watchLocation(function (loc) {
            if (loc) {
                console.log("Current location is: " + loc.latitude + ", " + loc.longitude);
                self.latitude = loc.latitude;
                self.longitude = loc.longitude;
            }
        }, function (e) {
            console.log("Error: " + e.message);
        },
        {desiredAccuracy: enums.Accuracy.any, updateDistance: 10, minimumUpdateTime: 1000});
    }
    

    【讨论】:

    • 使用以下表示法,您可以使用“this”而无需存储对它的引用: ...watchLocation((loc) => { ... this.latitude = loc.latitude; 。 .. })
    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 2017-11-09
    • 1970-01-01
    • 2019-08-04
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多