簡體   English   中英

將相同的事件發送到angular2中的不同函數

[英]send same event to different functions in angular2

我的主頁上有一個搜索表單,其中有輸入框,然后選擇下拉菜單。 用戶可以通過鍵入位置或html5地理位置搜索其他用戶。 因此,當用戶首次訪問該頁面時,它會詢問用戶是否讓應用知道其位置。 如果他們同意,則采用緯度經度和從下拉列表中選擇的選項並運行數據庫查詢。 但是我不確定我接下來的嘗試是否應該做到。 由於我還想在用戶進行地理定位或不進行地理位置搜索時綁定選擇。 我的選擇輸入是

<div class="input-group-btn">
  <select class="btn btn-danger btn-lg" style="border-radius: 0px;" bindon-ngModel="btype" on-ngModelChange="search($event)" on-ngModelChange="successPosition($event)" name="btype" >
      <option *ngFor="let btype of bloodtypes" [value]="btype">{{btype}}</option>
  </select>
</div>

我在angular2組件中的地理位置函數如下所示

geolocation: function(){
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(this.successPosition,
            this.failurePosition, { 
                                    enableHighAccuracy: true, 
                                    timeout:3000,
                                    maximumAge: 4000 });
    }
    else{
        return(false);
    }
},
successPosition: function(position){
    if(position.coords.latitude && position.coords.longitude){
        self.latitude = position.coords.latitude;
        self.longitude = position.coords.longitude;
    }else{
        self.btype = position;
    }
    window.location.replace("/users?utf8=✓&longitude=" + 
         encodeURIComponent(self.longitude) + "&latitude=" + 
         encodeURIComponent(self.latitude) + "&btype=" + 
         encodeURIComponent(self.btype));
    }, 

但是,當successPosition()接收到參數時,我無法同時分配latitudelongitudebtype 如果我指定latitudelongitude ,則btype是未定義的,反之亦然。 請告訴我這是應該怎么辦,或者還有另一種方法?

這是您要查找的內容:

successPosition: function(position){
     self.btype = position;

     self.latitude = (position.coords) ? position.coords.latitude : 'something else';
     self.longitude = (position.coords) ? position.coords.longitude : 'something else';


     window.location.replace("/users?utf8=✓&longitude=" + encodeURIComponent(self.longitude) + "&latitude=" + encodeURIComponent(self.latitude) + "&btype=" + encodeURIComponent(self.btype));
}

除了上述Sonicd300的答案之外,問題實際上出在successPosition函數中的if-else塊上。 在上面的答案中,他為您提供了一個選項,可以通過提供一個整數運算符來重寫該函數,請在此處詳細了解-Javascript 一行If ... else ... else if語句

您可以重寫它-

successPosition: function(position){
     self.btype = position;

     self.latitude = (position.coords) ? position.coords.latitude : 'something else';
     self.longitude = (position.coords) ? position.coords.longitude : 'something else';


     window.location.replace("/users?utf8=✓&longitude=" + encodeURIComponent(self.longitude) + "&latitude=" + encodeURIComponent(self.latitude) + "&btype=" + encodeURIComponent(self.btype));
}

通過編寫此代碼並分配默認的經度和緯度,以防用戶在頁面加載時未提供該信息。

successPosition: function(position) {
    self.btype = position;
    if (position.coords.latitude && position.coords.longitude){
        self.latitude = position.coords.latitude;
        self.longitude = position.coords.longitude;
    } else {
    self.latitude = 'assign some default latitude'; // you should never assume that this will always be provided by the user on page load
    self.longitude = 'assign some default longitute'
}
window.location.replace("/users?utf8=✓&longitude=" + 
     encodeURIComponent(self.longitude) + "&latitude=" + 
     encodeURIComponent(self.latitude) + "&btype=" + 
     encodeURIComponent(self.btype));
}

暫無
暫無

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

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