Angular-ui-bootstrap typeahead module extension

I am using angular-ui-boostrap typeahead to have people choose the person's name or add a new name if their choice is missing.

typeahead with three two names and an 'add new' option

At the moment I have changed getMatchesAsync

with my own code:

      if(scope.matches.length < 4 || scope.matches.length == undefined){
        scope.matches.push({
          id: getMatchId(matches.length),
          label: 'Add New +',
          model: 'new'
        });
      }

      

But I understand that this is not a very good long-term solution, especially when the component is updated.

Where should I put my code and how do I integrate it into the component? Typeahead module: https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js

+3


source to share


1 answer


Here's an example of what I suggested in the comments ...

someModule.filter('filterWithNew', function($filter) {
    return function(array, expression, comparator) {
        var matches = $filter('filter')(array, expression, comparator);

        if (matches.length < 4) {
            matches.push({
                label: 'Add new +',
                model: 'new'
            });
        }
        return matches;
    };
});

      



Then you can use

... typeahead="name.label for name in names | filterWithNew:$viewValue"

      

+4


source







All Articles