听取angularj ui选择的on-select事件

时间:2015-10-20 03:24:29

标签: angularjs angular-directive angular-ui-select

我正在创建一个自定义属性,它会将有角度的ui选择数据发送到服务器。但是,在我的自定义指令中,我需要绑定到on-select事件来实现此目的。

这是我的指令的html代码:

 <div class="component">
        <ui-select send-data ng-model="country.selected" theme="select2" ng-disabled="disabled" style="width: 300px;">
            <ui-select-match placeholder="Select or search a country in the list...">{{$select.selected.name}}</ui-select-match>
            <ui-select-choices repeat="country in countries | filter: $select.search">
                <span ng-bind-html="country.name | html"></span>
                <small ng-bind-html="country.code | html"></small>
            </ui-select-choices>
        </ui-select>
    </div>

请注意,该指令使用自定义指令send-data。 这是我的指示:

var app = angular.module('app');
app.directive('sendData', [
        '$compile', '$timeout', '$http', function ($compile, $timeout, $http) {
            return {
                require: 'ngModel',
                link: function (scope, el, attrs, ctrl) {                         
                    el.on('on-select', function () {
                        console.log("changed");
                    });                 
                }
            };
        }
]);

console.log("changed");语句未在上述指令中触发。 为什么会这样,我弄错了吗?

我知道我可以从控制器绑定到on-select事件。但我想在自定义属性中实现这一点。

1 个答案:

答案 0 :(得分:1)

你不能,on-select不是一个事件(比如click,keydown)所以你可以绑定一个监听器,ui-select implement it它们只是绑定一个控制器或父级别功能

因此,最好让ui-select处理on-select方法。

如果您在on-select中有任何DOM操作,那么在sendData指令中,您必须找到正确的li元素并设置click事件。