如何触发typeahead的enter事件

时间:2015-07-11 07:13:23

标签: angularjs

如何在typeahead中触发事件。我使用typeahead下面的代码,当用户在typeahead的下拉框中选择任何用户名时,我想在下面的文本框中填写user.full_name。你有什么建议在预先输入中应该使用什么事件。任何建议

<div class="row">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="form-group has-feedback" ng-class="showError(userform.username)">
              <label class="col-sm-4 control-label">{{'USER.USERNAME' | translate}}:</label>
              <div class="col-sm-8">
                <input type="text" name="username" 
                ng-model="user.username" required ng-init="setTimeout($element.focus(), 500)" 
                typeahead="user as user.username for user in users | filter:$viewValue | limitTo:8"
                typeahead-on-select="select($model)"
                class="form-control">
                <span ng-show="showError(userform.username)" class="glyphicon glyphicon-remove form-control-feedback"></span>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="form-group has-feedback" ng-class="showError(userform.full_name)">
              <label class="col-sm-4 control-label">{{'USER.FULLNAME' | translate}}:</label>
              <div class="col-sm-8">
                <input type="text" class="form-control" name="full_name" ng-model="user.full_name">
                <span ng-show="showError(userform.full_name)" class="glyphicon glyphicon-remove form-control-feedback"></span>
              </div>
            </div>
          </div>
        </div>

以下是我的控制器的一部分

.controller('AdminUsersController', ['$rootScope', '$scope', '$state', '$stateParams', '$modal', '$log', '$timeout', 'usersApi', 'policiesApi','workspacesApi', 'msg', '$filter',
  function($rootScope, $scope, $state, $stateParams, $modal, $log, $timeout, usersApi, policiesApi, workspacesApi,msg, $filter) {
    var self = this,
      limit = 100,
....

 $scope.select = function (item) {
      console.log('********* This is select ');
      $scope.user.full_name = item.full_name;
      console.log('foo', item);
    };
    // Finally initialize the page
    self.refresh();
...

1 个答案:

答案 0 :(得分:0)

假设您正在使用AngularStrap TypeAhead,
触发用户输入,你只需要做:

$scope.$on('$typeahead.select', function(event, value, index, elem){
     $scope.user.full_name = value;
});