0

I have a scenario as follows :

I have one searchbox and the user can enter two types of queries

Scenario One the Search Starts with i.e. A1234 Scenario Two the search Starts with i.e. B1234

Depending on one or two above I want to hit a different webservice.

Whats the best way to distinguish the search on the above input. Is it when the user clicks on my ng-submit I use a regular expression ?

Or should I have a scope variable on .watch() to find out as the user is typing ?

<form role="form" ng-submit="searchForm.$valid && searchCode()" name="searchForm" novalidate>
     <div class="input-group">
        <input type="text" class="form-control" ng-model="searchQuery" name="searchQuery" required autocomplete="off" spellcheck="false" focus>
          <input type="submit" value="Search" class="btn btn-default btn-sarch-bar">
     </div>
</form>
4

1 回答 1

0

如果在单击提交按钮时激活了搜索,那么您可以在提交功能中处理登录,并相应地点击适当的 Web 服务。这些方面的东西:

<form role="form" ng-submit="submit(searchForm.$valid, data)" name="searchForm" novalidate>
  <div class="input-group">
    <input type="text" class="form-control" ng-model="data.searchQuery" name="searchQuery" required autocomplete="off" spellcheck="false" focus>
    <input type="submit" value="Search" class="btn btn-default btn-sarch-bar">
  </div>
</form>

提交功能:

$scope.submit = function(isValid, data) {
  if(!isValid) return;

  //check data.searchQuery and access the appropriate webservice here
}

如果您使用 angularjs 1.3,并且希望在用户与输入的交互时执行搜索,您可能有兴趣向输入元素添加指令,并使用 $asyncValidators

yearofmoo有一个很棒的教程,关于使用指令的表单和验证器。

于 2014-12-16T12:14:04.677 回答