Skip to content

Instantly share code, notes, and snippets.

@IanSaunders
Forked from dancras/gist:5191040
Created March 19, 2013 15:49
Show Gist options
  • Save IanSaunders/5197227 to your computer and use it in GitHub Desktop.
Save IanSaunders/5197227 to your computer and use it in GitHub Desktop.
'use strict';
angular.module('angularTrainingApp')
.directive('multiple', function () {
return {
scope: true,
link: function (scope, element, attrs) {
element.multiselect({
// Replicate the native functionality on the elements so
// that angular can handle the changes for us.
onChange: function (optionElement, checked) {
optionElement.removeAttr('selected');
if (checked) {
optionElement.attr('selected', 'selected');
}
element.change();
}
});
// Watch for any changes to the length of our select element
scope.$watch(function () {
return element[0].length;
}, function () {
element.multiselect('rebuild');
});
// Watch for any changes from outside the directive and refresh
scope.$watch(attrs.ngModel, function () {
element.multiselect('refresh');
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment