如何触发输入密钥

时间:2017-04-14 16:51:24

标签: javascript jquery angularjs

这是我的HTML

的输入栏和搜索按钮
<div>
    <div class="input-group search-bar">
        <input type="text" class="form-control search-box" placeholder="Search people" autofocus="" ng-model="searchPeople">
        <a ui-sref="index.peoplesearch({searchPeople : searchPeople})" class="input-group-addon search-box btn-primary btn-search">
            <i class="glyphicon glyphicon-search"></i>
        </a>
    </div>
   <div ng-repeat = "people in searchResultsPeople">
          {{people.name}}
</div></div>

此搜索的控制器是

.controller('PeopleSearchController',function($scope,$http,$stateParams) {
var searchResultsPeople = $http.get(URL + SEARCH_PEOPLE + APIKEY + QUERY + searchPeople);
        searchResultsPeople.then(
            function (response) {
                $scope.searchResultsPeople = response.data.results;
                $scope.searchForPeople = true;
                console.log(response.data.results);
            },
            function(response) {
                $scope.message = "Error: "+response.status + " " + response.statusText;
            }
        );

我有一个搜索按钮的图标,当我在搜索栏中输入并单击按钮时,它工作正常。在输入框中输入一些文字后按Enter键时如何执行相同的功能?

1 个答案:

答案 0 :(得分:0)

您可以使用ng-keyupng-keydown来捕获用户输入并检查是否按下了回车键 - 如果是,请执行特定操作。

<强> HTML

<input type="text" class="form-control search-box" ng-keyup="myKeyupHandler($event);"
       placeholder="Search people" autofocus="" ng-model="searchPeople"/>

<强>控制器

$scope.myKeyupHandler = function (event) {
  var enter = 13;
  if (event.keyCode === enter) {
    // perform some action
  }
};