Skip to content

Instantly share code, notes, and snippets.

@egermano
Last active December 29, 2015 18:39
Show Gist options
  • Save egermano/7711815 to your computer and use it in GitHub Desktop.
Save egermano/7711815 to your computer and use it in GitHub Desktop.
Chosen Directive for AngularJS
angular.module('myApp')
.directive('chosen',function(){
'use strict';
var linker = function(scope, element, attrs) {
var config = scope.$eval(attrs['chosen']),
list = attrs['chosenCollection'];
_.extend(config, {inherit_select_classes: true});
scope.$watch(list, function(){
element.trigger('chosen:updated');
if (config['allow_single_deselect']) {
element.data('chosen').allow_single_deselect = true;
};
});
scope.$watch(function(){
if (!!$(element).attr('class')) {
return $(element).attr('class').replace(/[\s]|[\-]/g, '');
};
}, function(newValue, oldValue, scope) {
if (newValue.match(/error/)) {
element.data('chosen').container.addClass('error');
} else {
element.data('chosen').container.removeClass('error');
};
}, true);
element.chosen(config);
};
return {
restrict:'A',
link: linker
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment