Angular UI-Select为"标记"对象添加重复标记

Rah*_*ore 6 javascript jquery tagging angularjs ui-select

我正在使用ui-select库来进行"标记"功能.

我正在使用对象数组,其中每个对象都有id和name.它工作正常.

如果我输入不存在的标签,它会创建一个我想要的新标签,但我唯一的问题是如果用户输入已经存在的标签,它会同时显示新标签和现有标签.ui-select只有在尚未存在时才允许新标记.

在此输入图像描述

如果我输入算法,那么它应该选择/显示现有的"Algorithm"标签,而不是允许重复标签插入.

我无法找到任何设置.他们的标记示例页面ui-select标记示例也发生了同样的问题.我想这不是那样的.那么在ui-select中这是可能的,还是应该在我的代码中处理它?有解决方案吗

这是我的代码:

<ui-select multiple tagging="questionVm.addNewTag" theme="select2" ng-model="addQueVm.newQuestion.tags" class="mdl-select">
    <ui-select-match  placeholder="Enter tags">
        <span ng-bind="$item.name"></span>
    </ui-select-match>
    <ui-select-choices repeat="tag in (questionVm.allTags | filter: $select.search)">
        <span ng-bind="tag.name"></span>
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

zem*_*eng 3

您需要将标记函数更改为仅当标记不在不区分大小写的标记集中时返回值:

questionVm.addNewTag = function(tag){            
    // this could be done somewhere else for efficiency
    var lowerCaseTags = questionVm.allTags.map(
       function(obj){
          return obj.name.toLowerCase()
       }
    );

    // this keeps the new tag from being displayed unless it is really new
    if(!(tag.toLowerCase() in lowerCaseTags)){
        return {name: tag}
    }
}
Run Code Online (Sandbox Code Playgroud)