Javascript 扩展angular ui引导程序typeahead模块

Javascript 扩展angular ui引导程序typeahead模块,javascript,angularjs,angular-ui-bootstrap,angular-ui-typeahead,Javascript,Angularjs,Angular Ui Bootstrap,Angular Ui Typeahead,我使用angular ui boostrap的typeahead组件让人们选择一个人的名字,或者如果他们的选择不存在,就添加一个新名字 现在我用自己的代码修改了getMatchesAsync: if(scope.matches.length < 4 || scope.matches.length == undefined){ scope.matches.push({ id: getMatchId(matches.length),

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

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

      if(scope.matches.length < 4 || scope.matches.length == undefined){
        scope.matches.push({
          id: getMatchId(matches.length),
          label: 'Add New +',
          model: 'new'
        });
      }
if(scope.matches.length<4 | | scope.matches.length==未定义){
scope.matches.push({
id:getMatchId(matches.length),
标签:“添加新+”,
型号:“新”
});
}
但我意识到这不是一个好的长期解决方案,尤其是当组件更新时

我应该将代码放在哪里,如何将其集成到组件中?
Typeahead模块:

以下是我在评论中建议的示例

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

你就不能在源数据进入时对其进行操作吗?这是一个好主意,但有两个原因行不通:1。过滤和匹配在客户端完成。2.我没有实际获取结果,列表已经加载。我想可能是这样的。您仍然可以在
sourceArray
表达式中使用一个方法,该方法返回带有可选“new”模型的筛选集(如果需要异步处理,则使用承诺)。唯一的区别是你做的是过滤/匹配而不是typeahead。想想看,typeahead根本不做任何过滤。我只是想创建一个代理@Phil我只是想建议在那里设置一个过滤器。。我基本上也是这样做的
typeahead='items中的item | addNew'
,或者类似的codeNice,这很有效。由于我在标记中将person指定为person.full\u name,因此我还必须将值为“add New+”的“full\u name”添加到哈希中。@Ashbury cool。我确实有点冒昧地猜测了一下您的数据格式,但我想您应该知道该怎么做
... typeahead="name.label for name in names | filterWithNew:$viewValue"