数据成员的值在 Typescript 中没有改变

Posted

技术标签:

【中文标题】数据成员的值在 Typescript 中没有改变【英文标题】:Value of data members not changing inside the Typescript 【发布时间】:2017-06-28 11:38:50 【问题描述】:

我正在使用 ionic 2 并且有一个如下所示的类。我正在使用 locationServices 插件,不想使用 ionic 原生地理定位插件。

export class a
   location_acquiring:boolean;   
   location_available:boolean;
   constructor()
        this.location_acquiring=true;
        this.location_available=false;
               
   fun()
        //Here i am using some cordova plugins and setting the location latitude and longitude localstorage variables.
        let self=this;
        cordova.plugins.diagnostic.isLocationEnabled(function(enabled)                                    
            let selfa=self;
            cordova.plugins.diagnostic.isLocationAvailable(function(available)
                let selfb=selfa;
                cordova.plugins.locationServices.geolocation.watchPosition(function(position) 
                    //Now here although project is build without errors. but the values of the variables are not updated.
                    selfb.location_acquiring=false;
                    selfb.location_available=true;
                ,function(error));
            ,function(error));
        ,function(error));
    
   show_values()
        console.log(this.location_acquiring);
        console.log(this.location_available);
    

locationServices 插件中的变量更改不会反映在类变量中。

show_values() 函数的输出

是的 假的

【问题讨论】:

未经测试,可能不相关,但对于谷歌地图 API,您需要在 zone.js 中显式运行一些回调。如果您尝试在构造函数中注入 private __zone: NgZone(NgZone 位于 angular/core 中),然后执行以下操作:this._zone.run(() => selfb.location_acquiring=false; selfb.location_available=true; ); 【参考方案1】:

首先,您不再需要 let self = this; 技术,只需使用 TypeScript 的 lambda 语法 () => 而不是匿名函数即可访问 this(您的类“a”的实例)深处lambdas。

然后,确保您正在检查 Cordova 插件调用中是否发生错误(例如,通过将错误输出到调试控制台)以及为什么您的成功回调代码永远不会被执行。

试试下面的代码:

export class a
  location_acquiring: boolean = true;
  location_available: boolean = false;

  fun()
    //Here i am using some cordova plugins and setting the location latitude and longitude localstorage variables.
    cordova.plugins.diagnostic.isLocationEnabled((enabled) => 
        cordova.plugins.diagnostic.isLocationAvailable((available) => 
            cordova.plugins.locationServices.geolocation.watchPosition(function(position) 
                //Now here although project is build without errors. but the values of the variables are not updated.
                this.location_acquiring=false;
                this.location_available=true;
            , function(error)
              console.error('[watchPosition]', error);
            );
        , (error) => 
          console.error('[isLocationAvailable]', error);
        );
    , (error) => 
      console.error('[isLocationEnabled]', error);
    );
  

  show_values()
    console.log(this.location_acquiring);
    console.log(this.location_available);
  

希望对你有帮助。

【讨论】:

以上是关于数据成员的值在 Typescript 中没有改变的主要内容,如果未能解决你的问题,请参考以下文章

“MKMapItem”类型的值在 Swift 3 中没有成员“网站”

成员结构的值在作为指针传递给函数后丢失

为啥我的值在运行我的函数后没有改变? [复制]

为什么返回的值在Typescript中为'undefined'?

变量的值在observeSingleEvent 中没有变化

频谱 - 输入值在 Firefox 中没有改变