angular-ui / ui-select2

AngularJS wrapper for select2 (deprecated, use angular-ui/ui-select)
https://github.com/angular-ui/ui-select
MIT License
596 stars 443 forks source link

Add directives for select2 custom events #270

Open Soviut opened 10 years ago

Soviut commented 10 years ago

It would be worth-while having custom select2 event directives for select2 events such as select2-opening. I needed to create one of these for triggering an analytics event to detect opening, even if the user doesn't change the model.

The code is based heavily on angular's own event directives.

app.directive('select2Opening', function($parse, $rootScope) {
    return {
        restrict: 'A',
        compile: function($element, attr) {
            var fn = $parse(attr.select2Opening);
            return function(scope, element) {
                element.on('select2-opening', function(e) {
                    var callback = function() {
                        fn(scope, {$event: e});
                    };
                    scope.$apply(callback);
                });
            };
        }
    };
});