![](/img/trans.png)
[英]how to call the same function from two different places in angularjs
[英]How to call same function from different places using different parameters in javascript and still retain old values of variables of that function?
我的地理定位程序中有以下代碼。
當用戶允許設備獲取地理位置信息時,將調用geolocation()
函數,並且該geolocation
函數再次傳播successPosition
函數與提供緯度和經度的position
對象。 在successPosition
函數內部,我還需要另一個名為btype
(血液類型)的變量,因為我正在開發一個程序來查找最近的獻血者。
因此,在過去,我嘗試詢問用戶使用javascript prompt
方法搜索哪種血型,但是使用該方法,用戶將不得不手動鍵入血型,但我要讓用戶從下拉列表中選擇類型。 但是,如果我創建一個表單並詢問用戶類型並將對象傳遞給SuccessPosition
函數,則position
對象將變為未定義-這是因為這一次我們僅傳遞btype
,而沒有position
。 當position
對象被傳遞時,另一個變量btype
未定義。
請告訴我什么是解決問題的最佳方法。
geolocation: function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.successPosition, this.failurePosition, {
enableHighAccuracy: true,
timeout: 3000,
maximumAge: 4000
});
} else {
return (false);
}
},
successPosition: function(position) {
self.latitude = position.coords.latitude;
self.longitude = position.coords.longitude;
//self.btype = btype;
self.partial_url = ("/users?utf8=✓&longitude=" + (self.longitude)
+ "&latitude=" + (self.latitude)
+ "&btype=" + encodeURIComponent(self.btype));
window.location = self.partial_url;
}
您的successPosition
函數不必負責立即設置window.location
。 我認為,在您的情況下,最簡單的方法是讓successPosition
觸發表單,該表單允許用戶選擇其血液類型,並且將事件偵聽器綁定到form元素,當用戶選擇一個血液類型時,該事件偵聽器將調用具有血液類型的setWindowLocation
之類的東西。 現在,您將擁有地理位置和血型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.