Last active
December 29, 2015 18:39
-
-
Save egermano/7711815 to your computer and use it in GitHub Desktop.
Chosen Directive for AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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