1

イオン2を使用しており、次のようなクラスがあります。私はlocationServicesプラグインを使用していて、イオン性のネイティブgeolocationプラグインを使用したくありません。Typescript内でデータメンバーの値が変更されない

locationServicesプラグイン内の変数の変更は、クラス変数に反映されません。

show_values()関数の出力

+0

未テストで、無関係かもしれませんが、GoogleマップAPIの場合は、明示的にzone.js内のコールバックを実行する必要があります。コンストラクタ内で 'private __zone:NgZone'(NgZoneがangle/coreに入っています)を注入し、次に: ' this._zone.run(>)=> { selfb.location_acquiring = false; selfbを実行するとどうなりますか? location_available = true; }); ' –

答えて

0

まず、あなただけthisにアクセスするために、匿名関数の代わりに、活字体のラムダ構文() => {}を使用し、もうlet self = this;技術は必要ありません。 (クラス "a"のインスタンス)lambda内の深い。

次に、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); 
    } 
} 

はそれがお役に立てば幸いです。

関連する問題