【问题标题】:Add new option for ui-select2 in angular js在 Angular js 中为 ui-select2 添加新选项
【发布时间】:2013-08-26 15:50:40
【问题描述】:

我在 angularjs 中使用 ui-select2,它使用 jquery 的 select2 插件。我有一些预定义的列表,然后当我开始搜索任何内容时,如果在列表中找不到,我想添加一个新值。我正在做这样的事情:

var selectOptions = {
    formatNoMatches: function(term) {
                return "<a ng-click=\"addCountry('abc');\">Add new country</a>";
            }
 };

 $scope.addCountry = function(countryName) {
    console.log (' test');
 };

但是单击不起作用,并且 addCountry 中的控制台永远不会打印。知道如何在 Angular js 中为 ui-select2 添加新选项吗?

【问题讨论】:

  • 我很确定 ng-click 不起作用,因为这段代码不是由 angularjs 编译的,因为你在运行时输出它......你能给我们提供一个 plunker 或 fiddle 吗?这将帮助我们从您已有的东西中找到解决方案。
  • 我试过 $compile ("添加新国家") ($scope),但这会返回 [ object object] 而不是 html;

标签: jquery angularjs jquery-select2 ui-select2 angularjs-select2


【解决方案1】:

我已经做到了,不是通过单击,而是允许用户在 select2 中输入他们喜欢的内容并按 Enter:

<input type="text" ui-select2="selectOpts" ng-model="selected"/>

在coffeescript中,但我希望你能明白:

  $scope.selectOpts =
    maximumSelectionSize: 1
    data: $scope.someListOfData
    tags: true
    multiple: false
    createSearchChoice: (term) ->
      {id: term, text: term}

我希望这能让你走上正轨,基本的事情是设置“tags: true”

将 Select2 置于“标记”模式,用户可以在其中添加新的选择和 预先存在的标签是通过这个选项属性提供的,它是 要么

和 createSearchChoice(在 http://ivaynberg.github.io/select2/ 中搜索):

从用户的搜索词中创建一个新的可选选项。允许创建通过查询功能不可用的选项。当用户可以动态创建选择时很有用,例如“标记”用例

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 2012-12-29
    • 2019-02-09
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    相关资源
    最近更新 更多