Javascript 将同一事件发送到angular2中的不同函数

Javascript 将同一事件发送到angular2中的不同函数,javascript,html,angular,angular2-forms,angular4-forms,Javascript,Html,Angular,Angular2 Forms,Angular4 Forms,我在我的主页上有一个搜索表单,它有输入框和选择下拉列表。用户可以通过键入位置或html5地理位置来搜索其他用户。所以,当用户第一次访问该页面时,它会询问用户是否让应用程序知道他们的位置。若他们同意,那个么从下拉列表中选择纬度经度和所选选项并运行数据库查询。然而,我不确定我的以下尝试是否应该如此。因为我还想在用户使用或不使用地理位置搜索时绑定select。我的选择输入是 <div class="input-group-btn"> <select class="btn btn-

我在我的主页上有一个搜索表单,它有输入框和选择下拉列表。用户可以通过键入位置或html5地理位置来搜索其他用户。所以,当用户第一次访问该页面时,它会询问用户是否让应用程序知道他们的位置。若他们同意,那个么从下拉列表中选择纬度经度和所选选项并运行数据库查询。然而,我不确定我的以下尝试是否应该如此。因为我还想在用户使用或不使用地理位置搜索时绑定select。我的选择输入是

<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>

但是当
successPosition()
接收到参数时,我不能同时分配
纬度
经度
b类型
。如果我指定
纬度
经度
b类型
未定义,反之亦然。请告诉我这是我该怎么做还是有其他方法?

这就是你想要的:

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块。在上面的答案中,他为您提供了一个选项,通过提供一个十进制运算符来重写函数,请阅读这里的更多信息-

你可以重写这个-

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));
}

您能否将事件发送到单个函数。然后让那个函数调用其他函数?若我将事件发送给单个函数,那个么我还需要发送position对象来获取纬度和经度。我需要从latitude-latitude和B类型形成url,但这里的问题是,如果条件失败,则只分配self.B类型,并且self.latitude和self.latitude变得未定义:(检查更新的函数,如果以任何方式设置了position.coords on fail,但未定义lat lng,则必须执行类似于
position.coords.hasOwnProperty('latitude')的操作。)
在三元运算符的条件下,在“其他值”部分,我想检索self.lation和self.longitude的旧值。请格式化代码。然后分别更改self.lation/longitude的“其他值”,因为我假设您在全局范围内有self,因此self中有旧值。请nfirm编辑答案:DWait,但如果第一次self.latitude self.longitude失败,它将是未定义的,您没有以前的值,在if中分组这些值是正确的答案,不需要三元运算符。
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));
}