select2 : launch function when input change

260 Views Asked by At

I use a select2 tag to choose a selection of users from the database. The problem is that the users database is huge, I don't want to have a list of a thousand possibilities while using select2.

I want the possibilites to change only when I type in the input field. Like a ng-change, but ng-change only works when you add or remove a choice in your model.

Ex : I would have an empty list at beginning, and if I type "Mike" I would have all users who are called "Mike"

<select2 multiple placeholder="Choose users" ng-model="usersSelection" s2-options="user.id as user.name for user in users"></select2>

Do you know how I could do that ? select2 code is huge, I'm just searching for a simple solution, like a watch function.

Thanks, and sorry for my english.

1

There are 1 best solutions below

2
dns_nx On

I don't know angularjs, but can't you use the property minimumInputLength when initializing the select2 box?

$("#element").select2({
    minimumInputLength: 3,
    ...
});