【问题标题】:Angular UI-Select adding duplicate tag for "Tagging" ObjectsAngular UI-Select 为“标记”对象添加重复标记
【发布时间】:2017-02-17 11:36:59
【问题描述】:

我正在使用 ui-select 库来实现“标记”功能。

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

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

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

我找不到任何设置。他们的标记示例页面ui-select tagging example 上也发生了同样的问题。 我想这不是故意的。那么这可能在 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>

【问题讨论】:

    标签: javascript jquery angularjs tagging ui-select


    【解决方案1】:

    您将希望更改您的标记功能,使其仅在标记不在不区分大小写的标记集中时返回一个值:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多