【问题标题】:how to use select2-removed of select2 in angularjs for triggering event when input is cleared清除输入时如何在angularjs中使用select2-removed的select2来触发事件
【发布时间】:2014-02-24 09:11:58
【问题描述】:

我正在使用 ui-select2,但在使用 allowClear: true 时遇到问题。 allowClear 正在按预期工作,但需要在清除输入时触发事件。 我尝试通过 $watch 实现它,但没有成功。有没有办法在 angularjs 中使用 select2-removed 函数。

<select ui-select2="timeConfig" ng-model="time" data-placeholder="{{TIME.placeholder}}">
    <option value=""></option>
    <option ng-repeat="option in TIME.options" value="{{option.value}}">
{{option.title}}</option>
        </select>

//在控制器中

 $scope.timeConfig = {
    allowClear : true
    }

 $scope.$watch('time', function(newVal,oldVal) {
    if(newVal=='') {
        console.log("Value is changed to clear");
    }
    console.log("Changing and making new request");
    }, true);

当我点击清除按钮时 $watch 没有被触发(模型中的值保持不变)

【问题讨论】:

  • 清除按钮有什么作用?
  • 它将select2中的选定选项清除为占位符。
  • 请同时包含按钮标记和js。
  • select2在选择每个项目时,在其自己的小x(横跨)上添加清除按钮。 span>
  • 我明白了。你能创建一个现场演示来演示这个问题吗? (例如使用plunker

标签: javascript jquery angularjs ui-select2


【解决方案1】:

select2 事件没有选项(可以在源码中看到:https://github.com/angular-ui/ui-select2/blob/master/src/select2.js

请看简单的演示 - http://plnkr.co/edit/FDrgZncCEajRulrHmjz4?p=preview,您的代码运行良好,当我点击“清除”时,我可以在日志中看到“值已更改为清除”消息。

您的其他代码可能有问题?

【讨论】:

  • 感谢 plnkr... 帮助我调试代码。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
相关资源
最近更新 更多