【发布时间】:2018-02-01 18:12:51
【问题描述】:
我的主页中有一个搜索表单,其中包含输入框和选择下拉菜单。用户可以通过键入位置或通过 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() 收到参数时,我不能同时分配latitude、longitude 和btype。如果我分配latitude 和longitude,btype 是未定义的,反之亦然。请告诉我这是我该怎么做还是有其他方法?
【问题讨论】:
-
您能否将事件发送到单个函数。然后让那个函数调用其他函数?
-
如果我将事件发送到单个函数,那么我还需要发送位置对象来获取纬度和经度。我需要从纬度经度和 btype 形成 url
标签: javascript html angular angular2-forms angular4-forms