Skip to content

AngularJS input event trigger

I searched in internet, how to implement jQuery UI autocomplete into angularJS. I have found a very good sample and it is working. Here Sourcecode

HTML

<div ng-app='MyModule'>
    <div ng-controller='DefaultCtrl'>
        <input auto-complete ui-items="names" ng-model="selected">
        selected = {{selected}}
    </div>
</div>  

JS

function DefaultCtrl($scope) {
    $scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];
}

angular.module('MyModule', []).directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
    };
});

What I really get confused is, about trigger input event on the select event.

select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }

What is input for an event, I have never hear about it. I searched in Internet about input event but could not find something helpful.

Answer

A good way to think about it is like this: it’s a keyup event that filters out all keys which do not have an effect on the textual value of the input and fires when a completion is selected from a list of previously input values (thanks to @szeryf for the tip!). See this answer from stackoverflow for more details.