簡體   English   中英

如何在cordova函數中調用有角JavaScript函數

[英]How can I call a angular JavaScript function inside a cordova function

我試圖獲取一個返回true或false的函數,或者看看是否可以在此函數內調用另一個函數。

 checkLocationIsOn() { cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled) { console.log('GPS location is ' + (enabled ? 'enabled' : 'disabled')); }, function(error) { console.error('The following error occurred: ' + error); } ); // tslint:disable-next-line: max-line-length cordova.plugins.locationAccuracy.request(this.onRequestSuccess, this.onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY); } 
此功能檢查是否在用戶手機上啟用了用戶GPS,我需要它返回true或false或調用javascript函數。 我已經嘗試使用this.testfunction()調用另一個函數; 但是當我在設備上運行它時,它給我一個錯誤,說函數this.testfunction()未定義。

您丟失了上下文,有兩種方法:

  1. 使用粗箭頭功能
  2. 使用bind(this)

兩種方法的示例:

cordova.plugins.locationAccuracy.request(data => this.onRequestSuccess(data), this.onRequestFailure.bind(this), cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM