简体   繁体   中英

extending angular-ui-bootstrap typeahead module

I'm using angular-ui-boostrap's typeahead component to let people choose a person's name, or add a new name if their selection isn't present.

typeahead有三个两个名字和一个'add new'选项

Right now I modified 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 realize this is not a 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

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 should be able to use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM