javascript - 扩展 angular-ui-bootstrap typeahead 模块

标签 javascript angularjs angular-ui-bootstrap angular-ui-typeahead

我正在使用 angular-ui-boostrap 的 typeahead 组件让人们选择一个人的名字,或者如果他们的选择不存在则添加一个新名字。

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

现在我用自己的代码修改了 getMatchesAsync:

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

但我意识到这不是一个好的长期解决方案,尤其是在更新组件时。

我应该把我的代码放在哪里,我如何将它集成到组件中? 输入模块:https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js

最佳答案

这是我在评论中建议的示例...

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;
    };
});

那么你应该可以使用

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

关于javascript - 扩展 angular-ui-bootstrap typeahead 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30771420/

相关文章:

javascript - 我应该在 Electron 应用程序中使用视口(viewport)元吗?

javascript - for循环中对象中每个键的返回值

JavaScript:真正调整图像大小

angularjs - 使用不同的参数运行同一个套件 2 次

javascript - Angular ui 路由器选项卡 - 事件时隐藏 div

javascript - 使用 jQuery 检查有效的电子邮件

javascript - 删除范围内值更改时的计时器

php - Angularjs 和 slim 框架 JWT 身份验证和 token 刷新流程

html - 如何模糊背景图像 - css?

javascript - AngularJS - ui-bootstrap 分页错误