0

最近,我正在使用 angucomplete 在我的搜索中实现自动完成,现在我希望实现的所需输出是,每当我通过单击鼠标选择自动完成建议之一时,它都会导航到所需的 url,或者说 google.com测试目的。下面代码的问题是,当我单击文本框而不选择任何建议时,navToUrlByClick 函数将触发。由于作者刚刚开始探索 javascript,因此对此类问题有任何建议和抱歉。提前致谢!

这是我的 test.html 代码:

<div class="padded-row" ng-click="navToUrlByClick('http://www.google.com')">
     <div angucomplete-alt id="ex5"
       placeholder="Search projects"
       pause="50"
       selected-object="selectedProject"
       remote-url="http://localhost:5000/"
       remote-url-request-formatter="remoteUrlRequestFn"
       remote-url-data-field="items"
       title-field="subs_name"
       minlength="1"
       input-class="form-control form-control-small"
       match-class="highlight">
     </div>
</div>

这是我的 app.js 代码:

$scope.navToUrlByClick = function(str){
  if ((str != "") || (str != null))
  {
    console.log(str)
    //$window.location.href = str;
  }
}
4

1 回答 1

0

我通过使用回调和下面几行代码来解决这个问题并添加到我的 app.js

$scope.selectedProject = function(selected) {
  if (selected) {
    // window.alert('You have selected ' + selected.title);
    $window.location.href = "http://www.google.com";
  } else {
    console.log('cleared');
  }
};

结案!

于 2018-07-07T17:44:59.350 回答